@@ -8001,7 +8001,10 @@ def conditional_types(
8001
8001
None means no new information can be inferred.
8002
8002
If default is set it is returned instead.
8003
8003
"""
8004
- if proposed_type_ranges and len (proposed_type_ranges ) == 1 :
8004
+ if not proposed_type_ranges :
8005
+ return UninhabitedType (), default
8006
+
8007
+ if len (proposed_type_ranges ) == 1 :
8005
8008
# expand e.g. bool -> Literal[True] | Literal[False]
8006
8009
target = proposed_type_ranges [0 ].item
8007
8010
target = get_proper_type (target )
@@ -8012,7 +8015,11 @@ def conditional_types(
8012
8015
current_type = try_expanding_sum_type_to_union (current_type , enum_name )
8013
8016
8014
8017
current_type = get_proper_type (current_type )
8015
- if isinstance (current_type , UnionType ) and (default == current_type ):
8018
+ if (
8019
+ isinstance (current_type , UnionType )
8020
+ and not any (tr .is_upper_bound for tr in proposed_type_ranges )
8021
+ and (default in (current_type , None ))
8022
+ ):
8016
8023
# factorize over union types
8017
8024
# if we try to narrow A|B to C, we instead narrow A to C and B to C, and
8018
8025
# return the union of the results
@@ -8027,47 +8034,39 @@ def conditional_types(
8027
8034
]
8028
8035
# separate list of tuples into two lists
8029
8036
yes_types , no_types = zip (* result )
8030
- yes_type = make_simplified_union ([t for t in yes_types if t is not None ])
8031
- no_type = restrict_subtype_away (
8032
- current_type , yes_type , consider_runtime_isinstance = consider_runtime_isinstance
8033
- )
8034
-
8035
- return yes_type , no_type
8036
-
8037
- if proposed_type_ranges :
8037
+ proposed_type = make_simplified_union ([t for t in yes_types if t is not None ])
8038
+ else :
8038
8039
proposed_items = [type_range .item for type_range in proposed_type_ranges ]
8039
8040
proposed_type = make_simplified_union (proposed_items )
8040
- if isinstance (proposed_type , AnyType ):
8041
- # We don't really know much about the proposed type, so we shouldn't
8042
- # attempt to narrow anything. Instead, we broaden the expr to Any to
8043
- # avoid false positives
8044
- return proposed_type , default
8045
- elif not any (
8046
- type_range .is_upper_bound for type_range in proposed_type_ranges
8047
- ) and is_proper_subtype (current_type , proposed_type , ignore_promotions = True ):
8048
- # Expression is always of one of the types in proposed_type_ranges
8049
- return default , UninhabitedType ()
8050
- elif not is_overlapping_types (current_type , proposed_type , ignore_promotions = True ):
8051
- # Expression is never of any type in proposed_type_ranges
8052
- return UninhabitedType (), default
8053
- else :
8054
- # we can only restrict when the type is precise, not bounded
8055
- proposed_precise_type = UnionType .make_union (
8056
- [
8057
- type_range .item
8058
- for type_range in proposed_type_ranges
8059
- if not type_range .is_upper_bound
8060
- ]
8061
- )
8062
- remaining_type = restrict_subtype_away (
8063
- current_type ,
8064
- proposed_precise_type ,
8065
- consider_runtime_isinstance = consider_runtime_isinstance ,
8066
- )
8067
- return proposed_type , remaining_type
8041
+
8042
+ if isinstance (proposed_type , AnyType ):
8043
+ # We don't really know much about the proposed type, so we shouldn't
8044
+ # attempt to narrow anything. Instead, we broaden the expr to Any to
8045
+ # avoid false positives
8046
+ return proposed_type , default
8047
+ elif not any (
8048
+ type_range .is_upper_bound for type_range in proposed_type_ranges
8049
+ ) and is_proper_subtype (current_type , proposed_type , ignore_promotions = True ):
8050
+ # Expression is always of one of the types in proposed_type_ranges
8051
+ return default , UninhabitedType ()
8052
+ elif not is_overlapping_types (current_type , proposed_type , ignore_promotions = True ):
8053
+ # Expression is never of any type in proposed_type_ranges
8054
+ return UninhabitedType (), default
8068
8055
else :
8069
- # An isinstance check, but we don't understand the type
8070
- return current_type , default
8056
+ # we can only restrict when the type is precise, not bounded
8057
+ proposed_precise_type = UnionType .make_union (
8058
+ [
8059
+ type_range .item
8060
+ for type_range in proposed_type_ranges
8061
+ if not type_range .is_upper_bound
8062
+ ]
8063
+ )
8064
+ remaining_type = restrict_subtype_away (
8065
+ current_type ,
8066
+ proposed_precise_type ,
8067
+ consider_runtime_isinstance = consider_runtime_isinstance ,
8068
+ )
8069
+ return proposed_type , remaining_type
8071
8070
8072
8071
8073
8072
def conditional_types_to_typemaps (
0 commit comments