Skip to content

Commit

Permalink
Fix unreachable block crash
Browse files Browse the repository at this point in the history
  • Loading branch information
pkch committed Apr 14, 2017
1 parent fbd0b91 commit e48d983
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
2 changes: 2 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2677,6 +2677,8 @@ def or_conditional_maps(m1: TypeMap, m2: TypeMap) -> TypeMap:

def convert_to_typetype(type_map: TypeMap) -> TypeMap:
converted_type_map = {} # type: TypeMap
if type_map is None:
return None
for expr, typ in type_map.items():
if isinstance(typ, UnionType):
converted_type_map[expr] = UnionType([TypeType(t) for t in typ.items])
Expand Down
39 changes: 24 additions & 15 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -1423,31 +1423,40 @@ def f(x: Union[int, A], a: Type[A]) -> None:
[builtins fixtures/isinstancelist.pyi]


[case testIssubclassUnreachable]
from typing import Type, Sequence, Union
x: Type[str]
if issubclass(x, int):
reveal_type(x) # unreachable block


class X: pass
class Y(X): pass
class Z(X): pass

a: Union[Type[Y], Type[Z]]
if issubclass(a, X):
reveal_type(a) # E: Revealed type is 'Union[Type[__main__.Y], Type[__main__.Z]]'
else:
reveal_type(a) # unreachable block

[builtins fixtures/isinstancelist.pyi]


[case testIssubclass]
from typing import Type, ClassVar
from typing import Type

class Goblin:
level: int
pass

class GoblinAmbusher(Goblin):
job: ClassVar[str] = 'Ranger'
pass

def test_issubclass(cls: Type[Goblin]) -> None:
if issubclass(cls, GoblinAmbusher):
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]'
cls.level
cls.job
ga = cls()
ga.level = 15
ga.job
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance
else:
reveal_type(cls) # E: Revealed type is 'Type[__main__.Goblin]'
cls.level
cls.job # E: Type[Goblin] has no attribute "job"
g = cls()
g.level = 15
g.job # E: "Goblin" has no attribute "job"


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


[case testIssubclassTuple]
from typing import Type, ClassVar
from typing import Type

class Mob:
pass
Expand Down

0 comments on commit e48d983

Please sign in to comment.