Skip to content

Commit 0c63401

Browse files
Add more test cases for type narrowing (#20603)
Test cases added from #20277 (initial PR) --------- Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
1 parent 21d6773 commit 0c63401

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

test-data/unit/check-narrowing.test

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2939,6 +2939,10 @@ if type(x) == type(y) == int:
29392939
reveal_type(y) # N: Revealed type is "builtins.int"
29402940
reveal_type(x) # N: Revealed type is "builtins.int"
29412941

2942+
z: Any
2943+
if int == type(z) == int:
2944+
reveal_type(z) # N: Revealed type is "builtins.int"
2945+
29422946
[case testTypeEqualsCheckUsingIs]
29432947
# flags: --strict-equality --warn-unreachable
29442948
from typing import Any
@@ -2972,7 +2976,63 @@ def main(x: Union[B, C]):
29722976
if type(x) is A:
29732977
reveal_type(x) # E: Statement is unreachable
29742978
else:
2975-
reveal_type(x) # N: Revealed type is "Union[__main__.B, __main__.C]"
2979+
reveal_type(x) # N: Revealed type is "__main__.B | __main__.C"
2980+
[builtins fixtures/isinstance.pyi]
2981+
2982+
[case testTypeEqualsCheckUsingImplicitTypes-xfail]
2983+
from typing import Any
2984+
2985+
x: str
2986+
y: Any
2987+
z: object
2988+
if type(y) is type(x):
2989+
reveal_type(x) # N: Revealed type is "builtins.str"
2990+
reveal_type(y) # N: Revealed type is "builtins.str"
2991+
2992+
if type(x) is type(z):
2993+
reveal_type(x) # N: Revealed type is "builtins.str"
2994+
reveal_type(z) # N: Revealed type is "builtins.str"
2995+
2996+
[case testTypeEqualsCheckUsingDifferentSpecializedTypes]
2997+
# flags: --warn-unreachable
2998+
from collections import defaultdict
2999+
3000+
x: defaultdict
3001+
y: dict
3002+
z: object
3003+
if type(x) is type(y) is type(z):
3004+
reveal_type(x) # N: Revealed type is "collections.defaultdict[Any, Any]"
3005+
reveal_type(y) # N: Revealed type is "collections.defaultdict[Any, Any]"
3006+
reveal_type(z) # N: Revealed type is "collections.defaultdict[Any, Any]"
3007+
3008+
[case testUnionTypeEquality-xfail]
3009+
# flags: --strict-equality --warn-unreachable
3010+
from typing import Any, reveal_type
3011+
3012+
x: Any = ()
3013+
if type(x) == (int, str):
3014+
reveal_type(x) # E: Statement is unreachable
3015+
[builtins fixtures/tuple.pyi]
3016+
3017+
[case testTypeIntersectionWithConcreteTypes]
3018+
# flags: --warn-unreachable
3019+
class X: x = 1
3020+
class Y: y = 1
3021+
class Z(X, Y): ...
3022+
3023+
z = Z()
3024+
x: X = z
3025+
y: Y = z
3026+
if type(x) is type(y):
3027+
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.X" and "__main__.Y">"
3028+
reveal_type(y) # N: Revealed type is "__main__.<subclass of "__main__.Y" and "__main__.X">"
3029+
x.y + y.x
3030+
3031+
if isinstance(x, type(y)) and isinstance(y, type(x)):
3032+
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.X" and "__main__.Y">"
3033+
reveal_type(y) # N: Revealed type is "__main__.<subclass of "__main__.X" and "__main__.Y">"
3034+
x.y + y.x
3035+
29763036
[builtins fixtures/isinstance.pyi]
29773037

29783038
[case testTypeEqualsNarrowingUnionWithElse]

0 commit comments

Comments
 (0)