@@ -8394,43 +8394,42 @@ def conditional_types(
83948394
83958395 if isinstance (proper_type , AnyType ):
83968396 return proposed_type , current_type
8397- elif isinstance (proposed_type , AnyType ):
8397+ if isinstance (proposed_type , AnyType ):
83988398 # We don't really know much about the proposed type, so we shouldn't
83998399 # attempt to narrow anything. Instead, we broaden the expr to Any to
84008400 # avoid false positives
84018401 return proposed_type , default
8402- elif not any (type_range .is_upper_bound for type_range in proposed_type_ranges ) and (
8403- # concrete subtypes
8404- is_proper_subtype (current_type , proposed_type , ignore_promotions = True )
8402+ if not any (type_range .is_upper_bound for type_range in proposed_type_ranges ):
8403+ # concrete subtype
8404+ if is_proper_subtype (current_type , proposed_type , ignore_promotions = True ):
8405+ return default , UninhabitedType ()
8406+
84058407 # structural subtypes
8406- or (
8407- (
8408- isinstance (proposed_type , CallableType )
8409- or (isinstance (proposed_type , Instance ) and proposed_type .type .is_protocol )
8408+ if (
8409+ isinstance (proposed_type , CallableType )
8410+ or (isinstance (proposed_type , Instance ) and proposed_type .type .is_protocol )
8411+ ) and is_subtype (current_type , proposed_type , ignore_promotions = True ):
8412+ # Note: It's possible that current_type=`Any | Proto` while proposed_type=`Proto`
8413+ # so we cannot return `Never` for the else branch
8414+ remainder = restrict_subtype_away (
8415+ current_type ,
8416+ default if default is not None else proposed_type ,
8417+ consider_runtime_isinstance = consider_runtime_isinstance ,
84108418 )
8411- and is_subtype (current_type , proposed_type , ignore_promotions = True )
8412- )
8413- ):
8414- # Expression is always of one of the types in proposed_type_ranges
8415- return default , UninhabitedType ()
8416- elif not is_overlapping_types (current_type , proposed_type , ignore_promotions = True ):
8419+ return default , remainder
8420+ if not is_overlapping_types (current_type , proposed_type , ignore_promotions = True ):
84178421 # Expression is never of any type in proposed_type_ranges
84188422 return UninhabitedType (), default
8419- else :
8420- # we can only restrict when the type is precise, not bounded
8421- proposed_precise_type = UnionType .make_union (
8422- [
8423- type_range .item
8424- for type_range in proposed_type_ranges
8425- if not type_range .is_upper_bound
8426- ]
8427- )
8428- remaining_type = restrict_subtype_away (
8429- current_type ,
8430- proposed_precise_type ,
8431- consider_runtime_isinstance = consider_runtime_isinstance ,
8432- )
8433- return proposed_type , remaining_type
8423+ # we can only restrict when the type is precise, not bounded
8424+ proposed_precise_type = UnionType .make_union (
8425+ [type_range .item for type_range in proposed_type_ranges if not type_range .is_upper_bound ]
8426+ )
8427+ remaining_type = restrict_subtype_away (
8428+ current_type ,
8429+ proposed_precise_type ,
8430+ consider_runtime_isinstance = consider_runtime_isinstance ,
8431+ )
8432+ return proposed_type , remaining_type
84348433
84358434
84368435def conditional_types_to_typemaps (
0 commit comments