Skip to content

Commit 831a00c

Browse files
committed
Avoid narrowing type(x) to type(y: Any)
1 parent 60d136e commit 831a00c

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

mypy/checker.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6827,6 +6827,15 @@ def narrow_type_by_identity_equality(
68276827
expr = operands[j]
68286828

68296829
current_type_range = self.get_isinstance_type(expr)
6830+
if current_type_range is not None:
6831+
target_type = get_proper_type(
6832+
make_simplified_union([tr.item for tr in current_type_range])
6833+
)
6834+
if isinstance(target_type, AnyType):
6835+
# Avoid widening to Any for checks like `type(x) is type(y: Any)`.
6836+
# We patch this here because it is desirable to widen to any for cases like
6837+
# isinstance(x, (y: Any))
6838+
continue
68306839
if_map, else_map = conditional_types_to_typemaps(
68316840
expr_in_type_expr,
68326841
*self.conditional_types_with_intersection(

test-data/unit/check-narrowing.test

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3040,6 +3040,28 @@ z: Any
30403040
if int == type(z) == int:
30413041
reveal_type(z) # N: Revealed type is "builtins.int"
30423042

3043+
[case testTypeEqualsCheckWidening]
3044+
# flags: --strict-equality --warn-unreachable
3045+
from typing import Any
3046+
3047+
def f(x: str, y: Any, z: object):
3048+
if type(x) is type(y):
3049+
reveal_type(x) # N: Revealed type is "builtins.str"
3050+
reveal_type(y) # N: Revealed type is "builtins.str"
3051+
3052+
if type(x) == type(y):
3053+
reveal_type(x) # N: Revealed type is "builtins.str"
3054+
reveal_type(y) # N: Revealed type is "builtins.str"
3055+
3056+
if type(x) is type(z):
3057+
reveal_type(x) # N: Revealed type is "builtins.str"
3058+
reveal_type(z) # N: Revealed type is "builtins.str"
3059+
3060+
if type(x) == type(z):
3061+
reveal_type(x) # N: Revealed type is "builtins.str"
3062+
reveal_type(z) # N: Revealed type is "builtins.str"
3063+
[builtins fixtures/primitives.pyi]
3064+
30433065
[case testTypeEqualsCheckUsingIs]
30443066
# flags: --strict-equality --warn-unreachable
30453067
from typing import Any
@@ -3076,20 +3098,6 @@ def main(x: Union[B, C]):
30763098
reveal_type(x) # N: Revealed type is "__main__.B | __main__.C"
30773099
[builtins fixtures/isinstance.pyi]
30783100

3079-
[case testTypeEqualsCheckUsingImplicitTypes-xfail]
3080-
from typing import Any
3081-
3082-
x: str
3083-
y: Any
3084-
z: object
3085-
if type(y) is type(x):
3086-
reveal_type(x) # N: Revealed type is "builtins.str"
3087-
reveal_type(y) # N: Revealed type is "builtins.str"
3088-
3089-
if type(x) is type(z):
3090-
reveal_type(x) # N: Revealed type is "builtins.str"
3091-
reveal_type(z) # N: Revealed type is "builtins.str"
3092-
30933101
[case testTypeEqualsCheckUsingDifferentSpecializedTypes]
30943102
# flags: --warn-unreachable
30953103
from collections import defaultdict

0 commit comments

Comments
 (0)