Skip to content

Commit 6d136ed

Browse files
authored
Move tests to check-narrowing , improve them slightly (#20637)
1 parent 043977c commit 6d136ed

File tree

2 files changed

+130
-123
lines changed

2 files changed

+130
-123
lines changed

test-data/unit/check-isinstance.test

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,129 +2699,6 @@ else:
26992699
reveal_type(x) # N: Revealed type is "type[__main__.A]"
27002700
[builtins fixtures/isinstance.pyi]
27012701

2702-
[case testTypeEqualsCheck]
2703-
from typing import Any
2704-
2705-
y: Any
2706-
if type(y) == int:
2707-
reveal_type(y) # N: Revealed type is "builtins.int"
2708-
2709-
2710-
[case testMultipleTypeEqualsCheck]
2711-
from typing import Any
2712-
2713-
x: Any
2714-
y: Any
2715-
if type(x) == type(y) == int:
2716-
reveal_type(y) # N: Revealed type is "builtins.int"
2717-
reveal_type(x) # N: Revealed type is "builtins.int"
2718-
2719-
[case testTypeEqualsCheckUsingIs]
2720-
from typing import Any
2721-
2722-
y: Any
2723-
if type(y) is int:
2724-
reveal_type(y) # N: Revealed type is "builtins.int"
2725-
2726-
[case testTypeEqualsCheckUsingIsNonOverlapping]
2727-
# flags: --warn-unreachable
2728-
from typing import Union
2729-
2730-
y: str
2731-
if type(y) is int: # E: Subclass of "str" and "int" cannot exist: would have incompatible method signatures
2732-
y # E: Statement is unreachable
2733-
else:
2734-
reveal_type(y) # N: Revealed type is "builtins.str"
2735-
[builtins fixtures/isinstance.pyi]
2736-
2737-
[case testTypeEqualsCheckUsingIsNonOverlappingChild-xfail]
2738-
# flags: --warn-unreachable
2739-
from typing import Union
2740-
2741-
class A: ...
2742-
class B: ...
2743-
class C(A): ...
2744-
x: Union[B, C]
2745-
# C instance cannot be exactly its parent A, we need reversed subtyping relationship
2746-
# here (type(parent) is Child).
2747-
if type(x) is A:
2748-
reveal_type(x) # E: Statement is unreachable
2749-
else:
2750-
reveal_type(x) # N: Revealed type is "Union[__main__.B, __main__.C]"
2751-
[builtins fixtures/isinstance.pyi]
2752-
2753-
[case testTypeEqualsNarrowingUnionWithElse]
2754-
from typing import Union
2755-
2756-
x: Union[int, str]
2757-
if type(x) is int:
2758-
reveal_type(x) # N: Revealed type is "builtins.int"
2759-
else:
2760-
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
2761-
2762-
[case testTypeEqualsMultipleTypesShouldntNarrow]
2763-
# flags: --warn-unreachable
2764-
# make sure we don't do any narrowing if there are multiple types being compared
2765-
2766-
from typing import Union
2767-
2768-
x: Union[int, str]
2769-
if type(x) == int == str:
2770-
reveal_type(x) # E: Statement is unreachable
2771-
else:
2772-
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
2773-
2774-
# mypy thinks int isn't defined unless we include this
2775-
[builtins fixtures/primitives.pyi]
2776-
2777-
[case testTypeNotEqualsCheck]
2778-
from typing import Union
2779-
2780-
x: Union[int, str]
2781-
if type(x) != int:
2782-
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
2783-
else:
2784-
reveal_type(x) # N: Revealed type is "builtins.int"
2785-
2786-
# mypy thinks int isn't defined unless we include this
2787-
[builtins fixtures/primitives.pyi]
2788-
2789-
[case testTypeNotEqualsCheckUsingIsNot]
2790-
from typing import Union
2791-
2792-
x: Union[int, str]
2793-
if type(x) is not int:
2794-
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
2795-
else:
2796-
reveal_type(x) # N: Revealed type is "builtins.int"
2797-
2798-
[case testNarrowInElseCaseIfFinal]
2799-
from typing import final, Union
2800-
@final
2801-
class C:
2802-
pass
2803-
class D:
2804-
pass
2805-
2806-
x: Union[C, D]
2807-
if type(x) is C:
2808-
reveal_type(x) # N: Revealed type is "__main__.C"
2809-
else:
2810-
reveal_type(x) # N: Revealed type is "__main__.D"
2811-
[case testNarrowInIfCaseIfFinalUsingIsNot]
2812-
from typing import final, Union
2813-
@final
2814-
class C:
2815-
pass
2816-
class D:
2817-
pass
2818-
2819-
x: Union[C, D]
2820-
if type(x) is not C:
2821-
reveal_type(x) # N: Revealed type is "__main__.D"
2822-
else:
2823-
reveal_type(x) # N: Revealed type is "__main__.C"
2824-
28252702
[case testHasAttrExistingAttribute]
28262703
class C:
28272704
x: int

test-data/unit/check-narrowing.test

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2920,3 +2920,133 @@ def f2(x: Any) -> None:
29202920
return
29212921
reveal_type(x) # N: Revealed type is "Any"
29222922
[builtins fixtures/tuple.pyi]
2923+
2924+
[case testTypeEqualsCheck]
2925+
# flags: --strict-equality --warn-unreachable
2926+
from typing import Any
2927+
2928+
y: Any
2929+
if type(y) == int:
2930+
reveal_type(y) # N: Revealed type is "builtins.int"
2931+
2932+
[case testMultipleTypeEqualsCheck]
2933+
# flags: --strict-equality --warn-unreachable
2934+
from typing import Any
2935+
2936+
x: Any
2937+
y: Any
2938+
if type(x) == type(y) == int:
2939+
reveal_type(y) # N: Revealed type is "builtins.int"
2940+
reveal_type(x) # N: Revealed type is "builtins.int"
2941+
2942+
[case testTypeEqualsCheckUsingIs]
2943+
# flags: --strict-equality --warn-unreachable
2944+
from typing import Any
2945+
2946+
y: Any
2947+
if type(y) is int:
2948+
reveal_type(y) # N: Revealed type is "builtins.int"
2949+
2950+
[case testTypeEqualsCheckUsingIsNonOverlapping]
2951+
# flags: --strict-equality --warn-unreachable
2952+
from typing import Union
2953+
2954+
y: str
2955+
if type(y) is int: # E: Non-overlapping identity check (left operand type: "type[str]", right operand type: "type[int]") \
2956+
# E: Subclass of "str" and "int" cannot exist: would have incompatible method signatures
2957+
y # E: Statement is unreachable
2958+
else:
2959+
reveal_type(y) # N: Revealed type is "builtins.str"
2960+
[builtins fixtures/isinstance.pyi]
2961+
2962+
[case testTypeEqualsCheckUsingIsNonOverlappingChild-xfail]
2963+
# flags: --strict-equality --warn-unreachable
2964+
from typing import Union
2965+
2966+
class A: ...
2967+
class B: ...
2968+
class C(A): ...
2969+
def main(x: Union[B, C]):
2970+
# C instance cannot be exactly its parent A, we need reversed subtyping relationship
2971+
# here (type(parent) is Child).
2972+
if type(x) is A:
2973+
reveal_type(x) # E: Statement is unreachable
2974+
else:
2975+
reveal_type(x) # N: Revealed type is "Union[__main__.B, __main__.C]"
2976+
[builtins fixtures/isinstance.pyi]
2977+
2978+
[case testTypeEqualsNarrowingUnionWithElse]
2979+
# flags: --strict-equality --warn-unreachable
2980+
from typing import Union
2981+
2982+
x: Union[int, str]
2983+
if type(x) is int:
2984+
reveal_type(x) # N: Revealed type is "builtins.int"
2985+
else:
2986+
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
2987+
2988+
[case testTypeEqualsMultipleTypesShouldntNarrow]
2989+
# flags: --strict-equality --warn-unreachable
2990+
2991+
from typing import Union
2992+
2993+
x: Union[int, str]
2994+
if type(x) == int == str: # E: Non-overlapping equality check (left operand type: "type[int]", right operand type: "type[str]")
2995+
reveal_type(x) # E: Statement is unreachable
2996+
else:
2997+
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
2998+
2999+
[builtins fixtures/primitives.pyi]
3000+
3001+
[case testTypeNotEqualsCheck]
3002+
# flags: --strict-equality --warn-unreachable
3003+
from typing import Union
3004+
3005+
x: Union[int, str]
3006+
if type(x) != int:
3007+
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
3008+
else:
3009+
reveal_type(x) # N: Revealed type is "builtins.int"
3010+
3011+
# mypy thinks int isn't defined unless we include this
3012+
[builtins fixtures/primitives.pyi]
3013+
3014+
[case testTypeNotEqualsCheckUsingIsNot]
3015+
# flags: --strict-equality --warn-unreachable
3016+
from typing import Union
3017+
3018+
x: Union[int, str]
3019+
if type(x) is not int:
3020+
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
3021+
else:
3022+
reveal_type(x) # N: Revealed type is "builtins.int"
3023+
3024+
[case testNarrowInElseCaseIfFinal]
3025+
# flags: --strict-equality --warn-unreachable
3026+
from typing import final, Union
3027+
@final
3028+
class C:
3029+
pass
3030+
class D:
3031+
pass
3032+
3033+
x: Union[C, D]
3034+
if type(x) is C:
3035+
reveal_type(x) # N: Revealed type is "__main__.C"
3036+
else:
3037+
reveal_type(x) # N: Revealed type is "__main__.D"
3038+
3039+
[case testNarrowInIfCaseIfFinalUsingIsNot]
3040+
# flags: --strict-equality --warn-unreachable
3041+
from typing import final, Union
3042+
@final
3043+
class C:
3044+
pass
3045+
class D:
3046+
pass
3047+
3048+
x: Union[C, D]
3049+
if type(x) is not C:
3050+
reveal_type(x) # N: Revealed type is "__main__.D"
3051+
else:
3052+
reveal_type(x) # N: Revealed type is "__main__.C"

0 commit comments

Comments
 (0)