@@ -7994,11 +7994,13 @@ def conditional_types(
7994
7994
) -> tuple [Type | None , Type | None ]:
7995
7995
"""Takes in the current type and a proposed type of an expression.
7996
7996
7997
- Returns a 2-tuple: The first element is the proposed type, if the expression
7998
- can be the proposed type. The second element is the type it would hold
7999
- if it was not the proposed type, if any. UninhabitedType means unreachable.
8000
- None means no new information can be inferred. If default is set it is returned
8001
- instead."""
7997
+ Returns a 2-tuple:
7998
+ The first element is the proposed type, if the expression can be the proposed type.
7999
+ The second element is the type it would hold if it was not the proposed type, if any.
8000
+ UninhabitedType means unreachable.
8001
+ None means no new information can be inferred.
8002
+ If default is set it is returned instead.
8003
+ """
8002
8004
if proposed_type_ranges :
8003
8005
if len (proposed_type_ranges ) == 1 :
8004
8006
target = proposed_type_ranges [0 ].item
@@ -8010,24 +8012,26 @@ def conditional_types(
8010
8012
current_type = try_expanding_sum_type_to_union (current_type , enum_name )
8011
8013
proposed_items = [type_range .item for type_range in proposed_type_ranges ]
8012
8014
proposed_type = make_simplified_union (proposed_items )
8013
- if isinstance (proposed_type , AnyType ):
8015
+ current_type = get_proper_type (current_type )
8016
+ if isinstance (proposed_type , AnyType ) or isinstance (current_type , AnyType ):
8014
8017
# We don't really know much about the proposed type, so we shouldn't
8015
8018
# attempt to narrow anything. Instead, we broaden the expr to Any to
8016
8019
# avoid false positives
8017
8020
return proposed_type , default
8018
- elif not any (
8021
+ elif not any ( # handle concrete subtypes
8019
8022
type_range .is_upper_bound for type_range in proposed_type_ranges
8020
- ) and ( # concrete subtypes
8021
- is_proper_subtype (current_type , proposed_type , ignore_promotions = True )
8022
- or ( # structural subtypes
8023
- is_subtype (current_type , proposed_type , ignore_promotions = True )
8024
- and (
8025
- isinstance (proposed_type , CallableType )
8026
- or (isinstance (proposed_type , Instance ) and proposed_type .type .is_protocol )
8027
- )
8023
+ ) and is_proper_subtype (current_type , proposed_type , ignore_promotions = True ):
8024
+ # Expression is always of one of the types in proposed_type_ranges
8025
+ return default , UninhabitedType ()
8026
+ elif not any ( # handle structural subtypes
8027
+ type_range .is_upper_bound for type_range in proposed_type_ranges
8028
+ ) and (
8029
+ is_subtype (current_type , proposed_type , ignore_promotions = True )
8030
+ and (
8031
+ isinstance (proposed_type , CallableType )
8032
+ or (isinstance (proposed_type , Instance ) and proposed_type .type .is_protocol )
8028
8033
)
8029
8034
):
8030
- # Expression is always of one of the types in proposed_type_ranges
8031
8035
return default , UninhabitedType ()
8032
8036
elif not is_overlapping_types (current_type , proposed_type , ignore_promotions = True ):
8033
8037
# Expression is never of any type in proposed_type_ranges
0 commit comments