Skip to content

Commit a4db99e

Browse files
committed
Add failing unit tests
1 parent 408b6bd commit a4db99e

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

test-data/unit/check-isinstance.test

+120
Original file line numberDiff line numberDiff line change
@@ -1359,3 +1359,123 @@ def f(x: Union[int, str], typ: type) -> None:
13591359
x + 1 # E: Unsupported operand types for + (likely involving Union)
13601360
[builtins fixtures/isinstancelist.pyi]
13611361

1362+
[case testIssubclass]
1363+
from typing import Type, ClassVar
1364+
1365+
1366+
class Mob:
1367+
pass
1368+
1369+
class Goblin(Mob):
1370+
aggressive: bool = True
1371+
level: int
1372+
1373+
class GoblinAmbusher(Goblin):
1374+
job: ClassVar[str] = 'Ranger'
1375+
1376+
class GoblinDigger(Goblin):
1377+
job: ClassVar[str] = 'Thief'
1378+
1379+
1380+
def test_issubclass1(cls: Type[Mob]) -> None:
1381+
if issubclass(cls, Goblin):
1382+
cls.aggressive
1383+
cls.level
1384+
cls.job # E: Type[Goblin] has no attribute "job"
1385+
g = cls()
1386+
g.level = 15
1387+
g.job # E: "Goblin" has no attribute "job"
1388+
if issubclass(cls, GoblinAmbusher):
1389+
cls.aggressive
1390+
cls.level
1391+
cls.job
1392+
g = cls()
1393+
g.level = 15
1394+
g.job
1395+
g.job = 'Warrior' # E: Cannot assign to class variable "job" via instance
1396+
else:
1397+
cls.aggressive # E: Type[Mob] has no attribute "aggressive"
1398+
cls.job # E: Type[Mob] has no attribute "job"
1399+
cls.level # E: Type[Mob] has no attribute "level"
1400+
m = cls()
1401+
m.level = 15 # E: "Mob" has no attribute "level"
1402+
m.job # E: "Mob" has no attribute "job"
1403+
if issubclass(cls, GoblinAmbusher):
1404+
cls.aggressive
1405+
cls.job
1406+
cls.level
1407+
ga = cls()
1408+
ga.level = 15
1409+
ga.job
1410+
ga.job = 'Warrior' # E: Cannot assign to class variable "job" via instance
1411+
1412+
if issubclass(cls, GoblinAmbusher):
1413+
cls.aggressive
1414+
cls.level
1415+
cls.job
1416+
ga = cls()
1417+
ga.level = 15
1418+
ga.job
1419+
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance
1420+
1421+
if issubclass(cls, (Goblin, GoblinAmbusher)):
1422+
cls.aggressive
1423+
cls.level
1424+
cls.job # E: Some element of union has no attribute "job"
1425+
g = cls()
1426+
g.level = 15
1427+
g.job # E: "Goblin" has no attribute "job"
1428+
if issubclass(cls, GoblinAmbusher):
1429+
cls.aggressive
1430+
cls.level
1431+
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]'
1432+
cls.job
1433+
ga = cls()
1434+
ga.level = 15
1435+
ga.job
1436+
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance
1437+
else:
1438+
cls.aggressive # E: Type[Mob] has no attribute "aggressive"
1439+
cls.job # E: Type[Mob] has no attribute "job"
1440+
cls.level # E: Type[Mob] has no attribute "level"
1441+
m = cls()
1442+
m.level = 15 # E: "Mob" has no attribute "level"
1443+
m.job # E: "Mob" has no attribute "job"
1444+
if issubclass(cls, GoblinAmbusher):
1445+
cls.aggressive
1446+
cls.job
1447+
cls.level
1448+
ga = cls()
1449+
ga.level = 15
1450+
ga.job
1451+
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance
1452+
1453+
if issubclass(cls, (GoblinDigger, GoblinAmbusher)):
1454+
cls.aggressive
1455+
cls.level
1456+
cls.job
1457+
g = cls()
1458+
g.level = 15
1459+
g.job
1460+
g.job = "Warrior" # E: Cannot assign to class variable "job" via instance
1461+
1462+
1463+
def test_issubclass2(cls: Type[Goblin]) -> None:
1464+
if issubclass(cls, GoblinAmbusher):
1465+
cls.aggressive
1466+
cls.level
1467+
cls.job
1468+
ga = cls()
1469+
ga.level = 15
1470+
ga.job
1471+
ga.job = "Warrior" # E: Cannot assign to class variable "job" via instance
1472+
else:
1473+
cls.aggressive
1474+
cls.level
1475+
cls.job # E: Type[Goblin] has no attribute "job"
1476+
g = cls()
1477+
g.level = 15
1478+
g.job # E: "Goblin" has no attribute "job"
1479+
1480+
1481+
[builtins fixtures/isinstancelist.pyi]

test-data/unit/fixtures/isinstancelist.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class tuple: pass
1212
class function: pass
1313

1414
def isinstance(x: object, t: Union[type, Tuple]) -> bool: pass
15+
def issubclass(x: object, t: Union[type, Tuple]) -> bool: pass
1516

1617
@builtinclass
1718
class int:

0 commit comments

Comments
 (0)