Skip to content

Commit 12b409f

Browse files
committed
Handle X[T] - X[Any]
1 parent 922cbf6 commit 12b409f

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

mypy/subtypes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,8 @@ def restrict_subtype_away(t: Type, s: Type, *, consider_runtime_isinstance: bool
19531953
return UninhabitedType()
19541954
elif is_proper_subtype(t, s, ignore_promotions=True):
19551955
return UninhabitedType()
1956+
elif is_proper_subtype(t, s, ignore_promotions=True, erase_instances=True):
1957+
return UninhabitedType()
19561958
else:
19571959
return t
19581960

test-data/unit/check-typeis.test

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,41 @@ def main(a: object) -> None:
134134
reveal_type(a) # N: Revealed type is "Union[builtins.int, builtins.str]"
135135
[builtins fixtures/tuple.pyi]
136136

137-
[case testTypeIsUnionOfGenerics]
138-
from typing import List, Union
137+
[case testTypeIsUnionWithGeneric]
138+
from typing import Any, List, Sequence, Union
139139
from typing_extensions import TypeIs
140-
def is_foo(a: Union[List[int], List[str]]) -> TypeIs[List[str]]: pass
141-
def main(a: Union[List[int], List[str]]) -> None:
142-
if is_foo(a):
140+
141+
def is_int_list(a: object) -> TypeIs[List[int]]: pass
142+
def is_int_seq(a: object) -> TypeIs[Sequence[int]]: pass
143+
def is_seq(a: object) -> TypeIs[Sequence[Any]]: pass
144+
145+
def f1(a: Union[List[int], List[str]]) -> None:
146+
if is_int_list(a):
147+
reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
148+
else:
143149
reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]"
150+
reveal_type(a) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]"
151+
152+
def f2(a: Union[List[int], int]) -> None:
153+
if is_int_list(a):
154+
reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
144155
else:
156+
reveal_type(a) # N: Revealed type is "builtins.int"
157+
reveal_type(a) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.int]"
158+
159+
def f3(a: Union[List[bool], List[str]]) -> None:
160+
if is_int_seq(a):
161+
reveal_type(a) # N: Revealed type is "builtins.list[builtins.bool]"
162+
else:
163+
reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]"
164+
reveal_type(a) # N: Revealed type is "Union[builtins.list[builtins.bool], builtins.list[builtins.str]]"
165+
166+
def f4(a: Union[List[int], int]) -> None:
167+
if is_seq(a):
145168
reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]"
146-
reveal_type(a) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]"
169+
else:
170+
reveal_type(a) # N: Revealed type is "builtins.int"
171+
reveal_type(a) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.int]"
147172
[builtins fixtures/tuple.pyi]
148173

149174
[case testTypeIsNonzeroFloat]

0 commit comments

Comments
 (0)