Skip to content

Commit 28894ce

Browse files
committed
Narrow issubclass to Never
1 parent 6e97289 commit 28894ce

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

mypy/checker.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8221,11 +8221,16 @@ def convert_to_typetype(type_map: TypeMap) -> TypeMap:
82218221
t = typ
82228222
if isinstance(t, TypeVarType):
82238223
t = t.upper_bound
8224+
8225+
if isinstance(get_proper_type(t), UninhabitedType):
8226+
converted_type_map[expr] = UninhabitedType()
8227+
continue
82248228
# TODO: should we only allow unions of instances as per PEP 484?
8225-
if not isinstance(get_proper_type(t), (UnionType, Instance, NoneType)):
8229+
elif not isinstance(get_proper_type(t), (UnionType, Instance, NoneType)):
82268230
# unknown type; error was likely reported earlier
82278231
return {}
82288232
converted_type_map[expr] = TypeType.make_normalized(typ)
8233+
82298234
return converted_type_map
82308235

82318236

test-data/unit/check-isinstance.test

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,8 @@ from typing import Type, Sequence, Union
15341534

15351535
x: Type[str]
15361536
if issubclass(x, int): # E: Subclass of "str" and "int" cannot exist: would have incompatible method signatures
1537-
reveal_type(x) # N: Revealed type is "Type[builtins.str]"
1537+
reveal_type(x) # E: Statement is unreachable \
1538+
# N: Revealed type is "Never"
15381539

15391540
class X: pass
15401541
class Y(X): pass
@@ -1544,7 +1545,8 @@ a: Union[Type[Y], Type[Z]]
15441545
if issubclass(a, X):
15451546
reveal_type(a) # N: Revealed type is "Union[Type[__main__.Y], Type[__main__.Z]]"
15461547
else:
1547-
reveal_type(a) # N: Revealed type is "Union[Type[__main__.Y], Type[__main__.Z]]"
1548+
reveal_type(a) # E: Statement is unreachable \
1549+
# N: Revealed type is "Never"
15481550
[builtins fixtures/isinstancelist.pyi]
15491551

15501552
[case testIssubclasDestructuringUnions1]
@@ -2668,7 +2670,8 @@ x: Type[A]
26682670
if issubclass(x, B):
26692671
reveal_type(x) # N: Revealed type is "Type[__main__.<subclass of "__main__.A" and "__main__.B">]"
26702672
if issubclass(x, C): # E: Subclass of "A", "B", and "C" cannot exist: would have incompatible method signatures
2671-
reveal_type(x) # N: Revealed type is "Type[__main__.<subclass of "__main__.A" and "__main__.B">]"
2673+
reveal_type(x) # E: Statement is unreachable \
2674+
# N: Revealed type is "Never"
26722675
else:
26732676
reveal_type(x) # N: Revealed type is "Type[__main__.<subclass of "__main__.A" and "__main__.B">]"
26742677
else:

test-data/unit/check-narrowing.test

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,11 +1126,10 @@ T = TypeVar("T", A, B)
11261126

11271127
def f(cls: Type[T]) -> T:
11281128
if issubclass(cls, A):
1129-
reveal_type(cls) # N: Revealed type is "Type[__main__.A]" \
1130-
# N: Revealed type is "Type[__main__.B]"
1129+
reveal_type(cls) # N: Revealed type is "Type[__main__.A]"
11311130
x: bool
11321131
if x:
1133-
return A() # E: Incompatible return value type (got "A", expected "B")
1132+
return A()
11341133
else:
11351134
return B() # E: Incompatible return value type (got "B", expected "A")
11361135
assert False

0 commit comments

Comments
 (0)