Skip to content

Commit 0c2430f

Browse files
authored
Avoid widening to Any for checks like type(x) is type(y: Any) (#20663)
1 parent 8a1ebf3 commit 0c2430f

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

mypy/checker.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6830,6 +6830,13 @@ def narrow_type_by_identity_equality(
68306830
expr = operands[j]
68316831

68326832
current_type_range = self.get_isinstance_type(expr)
6833+
if current_type_range is not None:
6834+
target_type = make_simplified_union([tr.item for tr in current_type_range])
6835+
if isinstance(target_type, AnyType):
6836+
# Avoid widening to Any for checks like `type(x) is type(y: Any)`.
6837+
# We patch this here because it is desirable to widen to any for cases like
6838+
# isinstance(x, (y: Any))
6839+
continue
68336840
if_map, else_map = conditional_types_to_typemaps(
68346841
expr_in_type_expr,
68356842
*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
@@ -3094,6 +3094,28 @@ z: Any
30943094
if int == type(z) == int:
30953095
reveal_type(z) # N: Revealed type is "builtins.int"
30963096

3097+
[case testTypeEqualsCheckWidening]
3098+
# flags: --strict-equality --warn-unreachable
3099+
from typing import Any
3100+
3101+
def f(x: str, y: Any, z: object):
3102+
if type(x) is type(y):
3103+
reveal_type(x) # N: Revealed type is "builtins.str"
3104+
reveal_type(y) # N: Revealed type is "builtins.str"
3105+
3106+
if type(x) == type(y):
3107+
reveal_type(x) # N: Revealed type is "builtins.str"
3108+
reveal_type(y) # N: Revealed type is "builtins.str"
3109+
3110+
if type(x) is type(z):
3111+
reveal_type(x) # N: Revealed type is "builtins.str"
3112+
reveal_type(z) # N: Revealed type is "builtins.str"
3113+
3114+
if type(x) == type(z):
3115+
reveal_type(x) # N: Revealed type is "builtins.str"
3116+
reveal_type(z) # N: Revealed type is "builtins.str"
3117+
[builtins fixtures/primitives.pyi]
3118+
30973119
[case testTypeEqualsCheckUsingIs]
30983120
# flags: --strict-equality --warn-unreachable
30993121
from typing import Any
@@ -3130,20 +3152,6 @@ def main(x: Union[B, C]):
31303152
reveal_type(x) # N: Revealed type is "__main__.B | __main__.C"
31313153
[builtins fixtures/isinstance.pyi]
31323154

3133-
[case testTypeEqualsCheckUsingImplicitTypes-xfail]
3134-
from typing import Any
3135-
3136-
x: str
3137-
y: Any
3138-
z: object
3139-
if type(y) is type(x):
3140-
reveal_type(x) # N: Revealed type is "builtins.str"
3141-
reveal_type(y) # N: Revealed type is "builtins.str"
3142-
3143-
if type(x) is type(z):
3144-
reveal_type(x) # N: Revealed type is "builtins.str"
3145-
reveal_type(z) # N: Revealed type is "builtins.str"
3146-
31473155
[case testTypeEqualsCheckUsingDifferentSpecializedTypes]
31483156
# flags: --warn-unreachable
31493157
from collections import defaultdict

0 commit comments

Comments
 (0)