Skip to content

Commit f94ca45

Browse files
committed
Address CR
1 parent a4a7214 commit f94ca45

File tree

2 files changed

+23
-66
lines changed

2 files changed

+23
-66
lines changed

mypy/checker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,8 +2683,7 @@ def convert_to_typetype(type_map: TypeMap) -> TypeMap:
26832683
elif isinstance(typ, Instance):
26842684
converted_type_map[expr] = TypeType(typ)
26852685
else:
2686-
# TODO: verify this is ok
2687-
# unknown object, don't know how to convert
2686+
# unknown type; error was likely reported earlier
26882687
return {}
26892688
return converted_type_map
26902689

@@ -2734,7 +2733,8 @@ def find_isinstance_check(node: Expression,
27342733
elif isinstance(vartype, TypeType):
27352734
vartype = vartype.item
27362735
else:
2737-
# TODO: verify this is ok
2736+
# any other object whose type we don't know precisely
2737+
# for example, Any or Instance of type type
27382738
return {}, {} # unknown type
27392739
yes_map, no_map = conditional_type_map(expr, vartype, type)
27402740
yes_map, no_map = map(convert_to_typetype, (yes_map, no_map))

test-data/unit/check-isinstance.test

Lines changed: 20 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,13 +1434,15 @@ class GoblinAmbusher(Goblin):
14341434

14351435
def test_issubclass(cls: Type[Goblin]) -> None:
14361436
if issubclass(cls, GoblinAmbusher):
1437+
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]'
14371438
cls.level
14381439
cls.job
14391440
ga = cls()
14401441
ga.level = 15
14411442
ga.job
14421443
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance
14431444
else:
1445+
reveal_type(cls) # E: Revealed type is 'Type[__main__.Goblin]'
14441446
cls.level
14451447
cls.job # E: Type[Goblin] has no attribute "job"
14461448
g = cls()
@@ -1452,52 +1454,29 @@ def test_issubclass(cls: Type[Goblin]) -> None:
14521454

14531455

14541456
[case testIssubclassDeepHierarchy]
1455-
from typing import Type, ClassVar
1457+
from typing import Type
14561458

14571459
class Mob:
14581460
pass
14591461

14601462
class Goblin(Mob):
1461-
level: int
1463+
pass
14621464

14631465
class GoblinAmbusher(Goblin):
1464-
job: ClassVar[str] = 'Ranger'
1466+
pass
14651467

14661468
def test_issubclass(cls: Type[Mob]) -> None:
14671469
if issubclass(cls, Goblin):
1468-
cls.level
1469-
cls.job # E: Type[Goblin] has no attribute "job"
1470-
g = cls()
1471-
g.level = 15
1472-
g.job # E: "Goblin" has no attribute "job"
1470+
reveal_type(cls) # E: Revealed type is 'Type[__main__.Goblin]'
14731471
if issubclass(cls, GoblinAmbusher):
1474-
cls.level
1475-
cls.job
1476-
g = cls()
1477-
g.level = 15
1478-
g.job
1479-
g.job = 'Warrior' # E: Cannot assign to class variable "job" via instance
1472+
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]'
14801473
else:
1481-
cls.job # E: Type[Mob] has no attribute "job"
1482-
cls.level # E: Type[Mob] has no attribute "level"
1483-
m = cls()
1484-
m.level = 15 # E: "Mob" has no attribute "level"
1485-
m.job # E: "Mob" has no attribute "job"
1474+
reveal_type(cls) # E: Revealed type is 'Type[__main__.Mob]'
14861475
if issubclass(cls, GoblinAmbusher):
1487-
cls.job
1488-
cls.level
1489-
ga = cls()
1490-
ga.level = 15
1491-
ga.job
1492-
ga.job = 'Warrior' # E: Cannot assign to class variable "job" via instance
1476+
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]'
14931477

14941478
if issubclass(cls, GoblinAmbusher):
1495-
cls.level
1496-
cls.job
1497-
ga = cls()
1498-
ga.level = 15
1499-
ga.job
1500-
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance
1479+
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]'
15011480

15021481
[builtins fixtures/isinstancelist.pyi]
15031482

@@ -1509,50 +1488,25 @@ class Mob:
15091488
pass
15101489

15111490
class Goblin(Mob):
1512-
level: int
1491+
pass
15131492

15141493
class GoblinAmbusher(Goblin):
1515-
job: ClassVar[str] = 'Ranger'
1494+
pass
15161495

15171496
class GoblinDigger(Goblin):
1518-
job: ClassVar[str] = 'Thief'
1497+
pass
15191498

15201499
def test_issubclass(cls: Type[Mob]) -> None:
15211500
if issubclass(cls, (Goblin, GoblinAmbusher)):
1522-
cls.level
1523-
cls.job # E: Type[Goblin] has no attribute "job"
1524-
g = cls()
1525-
g.level = 15
1526-
g.job # E: "Goblin" has no attribute "job"
1501+
reveal_type(cls) # E: Revealed type is 'Type[__main__.Goblin]'
15271502
if issubclass(cls, GoblinAmbusher):
1528-
cls.level
15291503
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]'
1530-
cls.job
1531-
ga = cls()
1532-
ga.level = 15
1533-
ga.job
1534-
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance
15351504
else:
1536-
cls.job # E: Type[Mob] has no attribute "job"
1537-
cls.level # E: Type[Mob] has no attribute "level"
1538-
m = cls()
1539-
m.level = 15 # E: "Mob" has no attribute "level"
1540-
m.job # E: "Mob" has no attribute "job"
1505+
reveal_type(cls) # E: Revealed type is 'Type[__main__.Mob]'
15411506
if issubclass(cls, GoblinAmbusher):
1542-
cls.job
1543-
cls.level
1544-
ga = cls()
1545-
ga.level = 15
1546-
ga.job
1547-
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance
1548-
1507+
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]'
15491508
if issubclass(cls, (GoblinDigger, GoblinAmbusher)):
1550-
cls.level
1551-
cls.job
1552-
g = cls()
1553-
g.level = 15
1554-
g.job
1555-
g.job = "Warrior" # E: Cannot assign to class variable "job" via instance
1509+
reveal_type(cls) # E: Revealed type is 'Union[Type[__main__.GoblinDigger], Type[__main__.GoblinAmbusher]]'
15561510

15571511
[builtins fixtures/isinstancelist.pyi]
15581512

@@ -1565,11 +1519,14 @@ class MyIntList(List[int]): pass
15651519

15661520
def f(cls: Type[object]) -> None:
15671521
if issubclass(cls, MyList):
1522+
reveal_type(cls) # E: Revealed type is 'Type[__main__.MyList]'
15681523
cls()[0]
15691524
else:
1525+
reveal_type(cls) # E: Revealed type is 'Type[builtins.object]'
15701526
cls()[0] # E: Value of type "object" is not indexable
15711527

15721528
if issubclass(cls, MyIntList):
1529+
reveal_type(cls) # E: Revealed type is 'Type[__main__.MyIntList]'
15731530
cls()[0] + 1
15741531

15751532
[builtins fixtures/isinstancelist.pyi]

0 commit comments

Comments
 (0)