Skip to content

Commit 05f90ef

Browse files
committed
Model exact narrowing with type(x) checks
1 parent eba9a4c commit 05f90ef

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

mypy/checker.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6812,13 +6812,20 @@ def narrow_type_by_identity_equality(
68126812
# We patch this here because it is desirable to widen to any for cases like
68136813
# isinstance(x, (y: Any))
68146814
continue
6815+
6816+
arg_type = self.lookup_type(expr_in_type_expr)
6817+
if_type, else_type = self.conditional_types_with_intersection(
6818+
arg_type, [current_type_range], expr_in_type_expr
6819+
)
6820+
if if_type is not None and (
6821+
not current_type_range.is_upper_bound
6822+
and not is_equivalent(if_type, current_type_range.item)
6823+
):
6824+
# type(x) and x.__class__ checks must exact match
6825+
if_type = UninhabitedType()
6826+
68156827
if_map, else_map = conditional_types_to_typemaps(
6816-
expr_in_type_expr,
6817-
*self.conditional_types_with_intersection(
6818-
self.lookup_type(expr_in_type_expr),
6819-
[current_type_range],
6820-
expr_in_type_expr,
6821-
),
6828+
expr_in_type_expr, if_type, else_type
68226829
)
68236830

68246831
is_final = (

test-data/unit/check-narrowing.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3152,7 +3152,7 @@ else:
31523152
reveal_type(y) # N: Revealed type is "builtins.str"
31533153
[builtins fixtures/isinstance.pyi]
31543154

3155-
[case testTypeEqualsCheckUsingIsNonOverlappingChild-xfail]
3155+
[case testTypeEqualsCheckUsingIsNonOverlappingChild]
31563156
# flags: --strict-equality --warn-unreachable
31573157
from typing import Union
31583158

0 commit comments

Comments
 (0)