Skip to content

Commit e48d983

Browse files
committed
Fix unreachable block crash
1 parent fbd0b91 commit e48d983

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

mypy/checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,6 +2677,8 @@ def or_conditional_maps(m1: TypeMap, m2: TypeMap) -> TypeMap:
26772677

26782678
def convert_to_typetype(type_map: TypeMap) -> TypeMap:
26792679
converted_type_map = {} # type: TypeMap
2680+
if type_map is None:
2681+
return None
26802682
for expr, typ in type_map.items():
26812683
if isinstance(typ, UnionType):
26822684
converted_type_map[expr] = UnionType([TypeType(t) for t in typ.items])

test-data/unit/check-isinstance.test

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,31 +1423,40 @@ def f(x: Union[int, A], a: Type[A]) -> None:
14231423
[builtins fixtures/isinstancelist.pyi]
14241424

14251425

1426+
[case testIssubclassUnreachable]
1427+
from typing import Type, Sequence, Union
1428+
x: Type[str]
1429+
if issubclass(x, int):
1430+
reveal_type(x) # unreachable block
1431+
1432+
1433+
class X: pass
1434+
class Y(X): pass
1435+
class Z(X): pass
1436+
1437+
a: Union[Type[Y], Type[Z]]
1438+
if issubclass(a, X):
1439+
reveal_type(a) # E: Revealed type is 'Union[Type[__main__.Y], Type[__main__.Z]]'
1440+
else:
1441+
reveal_type(a) # unreachable block
1442+
1443+
[builtins fixtures/isinstancelist.pyi]
1444+
1445+
14261446
[case testIssubclass]
1427-
from typing import Type, ClassVar
1447+
from typing import Type
14281448

14291449
class Goblin:
1430-
level: int
1450+
pass
14311451

14321452
class GoblinAmbusher(Goblin):
1433-
job: ClassVar[str] = 'Ranger'
1453+
pass
14341454

14351455
def test_issubclass(cls: Type[Goblin]) -> None:
14361456
if issubclass(cls, GoblinAmbusher):
14371457
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]'
1438-
cls.level
1439-
cls.job
1440-
ga = cls()
1441-
ga.level = 15
1442-
ga.job
1443-
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance
14441458
else:
14451459
reveal_type(cls) # E: Revealed type is 'Type[__main__.Goblin]'
1446-
cls.level
1447-
cls.job # E: Type[Goblin] has no attribute "job"
1448-
g = cls()
1449-
g.level = 15
1450-
g.job # E: "Goblin" has no attribute "job"
14511460

14521461

14531462
[builtins fixtures/isinstancelist.pyi]
@@ -1482,7 +1491,7 @@ def test_issubclass(cls: Type[Mob]) -> None:
14821491

14831492

14841493
[case testIssubclassTuple]
1485-
from typing import Type, ClassVar
1494+
from typing import Type
14861495

14871496
class Mob:
14881497
pass

0 commit comments

Comments
 (0)