Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 64 additions & 5 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -2707,7 +2707,7 @@ if type(y) == int:
reveal_type(y) # N: Revealed type is "builtins.int"


[case testMultipleTypeEqualsCheck]
[case testMultipleTypeEqualsCheck-xfail]
from typing import Any

x: Any
Expand All @@ -2716,14 +2716,73 @@ if type(x) == type(y) == int:
reveal_type(y) # N: Revealed type is "builtins.int"
reveal_type(x) # N: Revealed type is "builtins.int"

z: Any
if int == type(z) == int:
reveal_type(z) # N: Revealed type is "builtins.int"

[case testTypeEqualsCheckUsingIs]
from typing import Any

y: Any
if type(y) is int:
reveal_type(y) # N: Revealed type is "builtins.int"

[case testTypeEqualsCheckUsingIsNonOverlapping]
[case testTypeEqualsCheckUsingImplicitTypes-xfail]
from typing import Any

x: str
y: Any
z: object
if type(y) is type(x):
reveal_type(x) # N: Revealed type is "builtins.str"
reveal_type(y) # N: Revealed type is "builtins.str"

if type(x) is type(z):
reveal_type(x) # N: Revealed type is "builtins.str"
reveal_type(z) # N: Revealed type is "builtins.str"

[case testTypeEqualsCheckUsingDifferentSpecializedTypes-xfail]
from collections import defaultdict

x: defaultdict
y: dict
z: object
if type(x) is type(y) is type(z):
reveal_type(x) # N: Revealed type is "collections.defaultdict[Any, Any]"
reveal_type(y) # N: Revealed type is "collections.defaultdict[Any, Any]"
reveal_type(z) # N: Revealed type is "collections.defaultdict[Any, Any]"

[case testUnionTypeEquality-xfail]
from typing import Any, reveal_type
# flags: --warn-unreachable

x: Any = ()
if type(x) == (int, str):
reveal_type(x) # E: Statement is unreachable

[builtins fixtures/tuple.pyi]

[case testTypeIntersectionWithConcreteTypes-xfail]
class X: x = 1
class Y: y = 1
class Z(X, Y): ...

z = Z()
x: X = z
y: Y = z
if type(x) is type(y):
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.X" and "__main__.Y">"
reveal_type(y) # N: Revealed type is "__main__.<subclass of "__main__.Y" and "__main__.X">"
x.y + y.x

if isinstance(x, type(y)) and isinstance(y, type(x)):
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.X" and "__main__.Y">"
reveal_type(y) # N: Revealed type is "__main__.<subclass of "__main__.X" and "__main__.Y">"
x.y + y.x

[builtins fixtures/isinstance.pyi]

[case testTypeEqualsCheckUsingIsNonOverlapping-xfail]
# flags: --warn-unreachable
from typing import Union

Expand All @@ -2732,7 +2791,6 @@ if type(y) is int: # E: Subclass of "str" and "int" cannot exist: would have in
y # E: Statement is unreachable
else:
reveal_type(y) # N: Revealed type is "builtins.str"
[builtins fixtures/isinstance.pyi]

[case testTypeEqualsCheckUsingIsNonOverlappingChild-xfail]
# flags: --warn-unreachable
Expand All @@ -2759,15 +2817,16 @@ if type(x) is int:
else:
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"

[case testTypeEqualsMultipleTypesShouldntNarrow]
[case testTypeEqualsMultipleTypesShouldntNarrow-xfail]
# flags: --warn-unreachable
# make sure we don't do any narrowing if there are multiple types being compared
# flags: --warn-unreachable

from typing import Union

x: Union[int, str]
if type(x) == int == str:
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
reveal_type(x) # E: Statement is unreachable
else:
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ def f(t: Type[C]) -> None:
if type(t) is M:
reveal_type(t) # N: Revealed type is "type[__main__.C]"
else:
reveal_type(t) # N: Revealed type is "type[__main__.C]"
reveal_type(t) # N: Revealed type is "type[__main__.C]"
if type(t) is not M:
reveal_type(t) # N: Revealed type is "type[__main__.C]"
else:
Expand Down