Skip to content

Commit 635a82b

Browse files
committed
Revert "revert"
This reverts commit 0473058.
1 parent 4d6be0d commit 635a82b

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

mypy/checker.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6719,13 +6719,15 @@ def narrow_type_by_identity_equality(
67196719

67206720
if is_target_for_value_narrowing(get_proper_type(target_type)):
67216721
if_map, else_map = conditional_types_to_typemaps(
6722-
operands[i], *conditional_types(expanded_expr_type, [target])
6722+
operands[i],
6723+
*conditional_types(expanded_expr_type, [target], ignore_promotions=False),
67236724
)
67246725
all_if_maps.append(if_map)
67256726
all_else_maps.append(else_map)
67266727
else:
67276728
if_map, else_map = conditional_types_to_typemaps(
6728-
operands[i], *conditional_types(expr_type, [target])
6729+
operands[i],
6730+
*conditional_types(expr_type, [target], ignore_promotions=False),
67296731
)
67306732
# For value targets, it is safe to narrow in the negative case.
67316733
# e.g. if (x: Literal[5] | None) != (y: Literal[5]), we can narrow x to None
@@ -6756,7 +6758,8 @@ def narrow_type_by_identity_equality(
67566758
target = TypeRange(target_type, is_upper_bound=False)
67576759
if is_target_for_value_narrowing(get_proper_type(target_type)):
67586760
if_map, else_map = conditional_types_to_typemaps(
6759-
operands[i], *conditional_types(expr_type, [target])
6761+
operands[i],
6762+
*conditional_types(expr_type, [target], ignore_promotions=False),
67606763
)
67616764
if else_map:
67626765
all_else_maps.append(else_map)
@@ -6786,7 +6789,10 @@ def narrow_type_by_identity_equality(
67866789
expr_type = coerce_to_literal(expr_type)
67876790
expr_type = try_expanding_sum_type_to_union(expr_type, None)
67886791
if_map, else_map = conditional_types_to_typemaps(
6789-
operands[i], *conditional_types(expr_type, [target], default=expr_type)
6792+
operands[i],
6793+
*conditional_types(
6794+
expr_type, [target], default=expr_type, ignore_promotions=False
6795+
),
67906796
)
67916797
or_if_maps.append(if_map)
67926798
if is_value_target:
@@ -8244,6 +8250,7 @@ def conditional_types(
82448250
default: None = None,
82458251
*,
82468252
consider_runtime_isinstance: bool = True,
8253+
ignore_promotions: bool = True,
82478254
) -> tuple[Type | None, Type | None]: ...
82488255

82498256

@@ -8254,6 +8261,7 @@ def conditional_types(
82548261
default: Type,
82558262
*,
82568263
consider_runtime_isinstance: bool = True,
8264+
ignore_promotions: bool = True,
82578265
) -> tuple[Type, Type]: ...
82588266

82598267

@@ -8263,6 +8271,7 @@ def conditional_types(
82638271
default: Type | None = None,
82648272
*,
82658273
consider_runtime_isinstance: bool = True,
8274+
ignore_promotions: bool = True,
82668275
) -> tuple[Type | None, Type | None]:
82678276
"""Takes in the current type and a proposed type of an expression.
82688277
@@ -8302,6 +8311,7 @@ def conditional_types(
83028311
proposed_type_ranges,
83038312
default=union_item,
83048313
consider_runtime_isinstance=consider_runtime_isinstance,
8314+
ignore_promotions=ignore_promotions,
83058315
)
83068316
for union_item in get_proper_types(proper_type.items)
83078317
]
@@ -8321,14 +8331,14 @@ def conditional_types(
83218331
return proposed_type, default
83228332
if not any(type_range.is_upper_bound for type_range in proposed_type_ranges):
83238333
# concrete subtype
8324-
if is_proper_subtype(current_type, proposed_type, ignore_promotions=True):
8334+
if is_proper_subtype(current_type, proposed_type, ignore_promotions=ignore_promotions):
83258335
return default, UninhabitedType()
83268336

83278337
# structural subtypes
83288338
if (
83298339
isinstance(proposed_type, CallableType)
83308340
or (isinstance(proposed_type, Instance) and proposed_type.type.is_protocol)
8331-
) and is_subtype(current_type, proposed_type, ignore_promotions=True):
8341+
) and is_subtype(current_type, proposed_type, ignore_promotions=ignore_promotions):
83328342
# Note: It's possible that current_type=`Any | Proto` while proposed_type=`Proto`
83338343
# so we cannot return `Never` for the else branch
83348344
remainder = restrict_subtype_away(
@@ -8337,7 +8347,7 @@ def conditional_types(
83378347
consider_runtime_isinstance=consider_runtime_isinstance,
83388348
)
83398349
return default, remainder
8340-
if not is_overlapping_types(current_type, proposed_type, ignore_promotions=True):
8350+
if not is_overlapping_types(current_type, proposed_type, ignore_promotions=ignore_promotions):
83418351
# Expression is never of any type in proposed_type_ranges
83428352
return UninhabitedType(), default
83438353
# we can only restrict when the type is precise, not bounded

0 commit comments

Comments
 (0)