File: run-classes.test

package info (click to toggle)
mypy 0.812-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 18,596 kB
  • sloc: python: 74,869; cpp: 11,212; ansic: 3,935; makefile: 238; sh: 13
file content (1770 lines) | stat: -rw-r--r-- 34,420 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
[case testEmptyClass]
class Empty: pass

def f(e: Empty) -> Empty:
    return e
[file driver.py]
from native import Empty, f

print(isinstance(Empty, type))
print(Empty)
print(str(Empty())[:20])

e = Empty()
print(f(e) is e)
[out]
True
<class 'native.Empty'>
<native.Empty object
True

[case testClassWithFields]
class C:
    x: int
    y: int
    z: 'D'
class D:
    pass
[file driver.py]
from native import C

c = C()
assert not hasattr(c, 'x')
assert not hasattr(c, 'y')
c.x = 1
c.y = 2
print(c.x)
print(c.y)
c.x = 10**30
print(c.x)
c.x = 10**30+1
print(c.x)
assert hasattr(c, 'x')
assert hasattr(c, 'y')
assert not hasattr(c, 'z')
del c.x
assert not hasattr(c, 'x')
assert hasattr(c, 'y')
del c.y
assert not hasattr(c, 'y')
c.x = 10**30+2
print(c.x)
assert hasattr(c, 'x')
[out]
1
2
1000000000000000000000000000000
1000000000000000000000000000001
1000000000000000000000000000002

[case testNonExtMisc]
from typing import Any, overload

def decorator(cls) -> Any:
    return cls

@decorator
class C:
    def __init__(self) -> None:
        self.c = 3

    def get_c(self) -> int:
        return self.c

@decorator
class B(C):
    def __init__(self) -> None:
        super().__init__()
        self.b = 2

    def get_b(self) -> int:
        return self.b

@decorator
class A(B):
    def __init__(self) -> None:
        super().__init__()
        self.a = 1

    @classmethod
    def constant(cls) -> int:
        return 4

    def get_a(self) -> int:
        return self.a

@decorator
class Overload:
    @overload
    def get(self, index: int) -> int: ...

    @overload
    def get(self, index: str) -> str: ...

    def get(self, index: Any) -> Any:
        return index

def get(c: Overload, s: str) -> str:
    return c.get(s)

[file driver.py]
from native import A, Overload, get
a = A()
assert a.a == 1
assert a.b == 2
assert a.c == 3
assert a.get_a() == 1
assert a.get_b() == 2
assert a.get_c() == 3
assert A.constant() == 4

o = Overload()
assert get(o, "test") == "test"
assert o.get(20) == 20

[case testEnum]
from enum import Enum

class TestEnum(Enum):
    _order_ = "a b"
    a : int = 1
    b : int = 2

    @classmethod
    def test(cls) -> int:
        return 3

assert TestEnum.test() == 3

[file other.py]
# Force a multi-module test to make sure we can compile multi-file with
# non-extension classes

[file driver.py]
import sys
# "_order_" isn't supported in 3.5
if sys.version_info[:2] > (3, 5):
    from native import TestEnum
    assert TestEnum.a.name == 'a'
    assert TestEnum.a.value == 1
    assert TestEnum.b.name == 'b'
    assert TestEnum.b.value == 2

[case testRunDataclass]
from dataclasses import dataclass, field
from typing import Set, List, Callable, Any

@dataclass
class Person1:
    age : int
    name : str

    def __bool__(self) -> bool:
        return self.name == 'robot'

def testBool(p: Person1) -> bool:
    if p:
        return True
    else:
        return False

@dataclass
class Person1b(Person1):
    id: str = '000'

@dataclass
class Person2:
    age : int
    name : str = field(default='robot')

@dataclass(order = True)
class Person3:
    age : int = field(default = 6)
    friendIDs : List[int] = field(default_factory = list)

    def get_age(self) -> int:
        return (self.age)

    def set_age(self, new_age : int) -> None:
        self.age = new_age

    def add_friendID(self, fid : int) -> None:
        self.friendIDs.append(fid)

    def get_friendIDs(self) -> List[int]:
        return self.friendIDs

def get_next_age(g: Callable[[Any], int]) -> Callable[[Any], int]:
    def f(a: Any) -> int:
        return g(a) + 1
    return f

@dataclass
class Person4:
    age : int
    _name : str = 'Bot'

    @get_next_age
    def get_age(self) -> int:
        return self.age

    @property
    def name(self) -> str:
        return self._name

[file other.py]
from native import Person1, Person1b, Person2, Person3, Person4, testBool
i1 = Person1(age = 5, name = 'robot')
assert i1.age == 5
assert i1.name == 'robot'
assert testBool(i1) == True
assert testBool(Person1(age = 5, name = 'robo')) == False
i1b = Person1b(age = 5, name = 'robot')
assert i1b.age == 5
assert i1b.name == 'robot'
assert testBool(i1b) == True
assert testBool(Person1b(age = 5, name = 'robo')) == False
i1c = Person1b(age = 20, name = 'robot', id = 'test')
assert i1c.age == 20
assert i1c.id == 'test'

i2 = Person2(age = 5)
assert i2.age == 5
assert i2.name == 'robot'
i3 = Person2(age = 5, name = 'new_robot')
assert i3.age == 5
assert i3.name == 'new_robot'
i4 = Person3()
assert i4.age == 6
assert i4.friendIDs == []
i5 = Person3(age = 5)
assert i5.age == 5
assert i5.friendIDs == []
i6 = Person3(age = 5, friendIDs = [1,2,3])
assert i6.age == 5
assert i6.friendIDs == [1,2,3]
assert i6.get_age() == 5
i6.set_age(10)
assert i6.get_age() == 10
i6.add_friendID(4)
assert i6.get_friendIDs() == [1,2,3,4]
i7 = Person4(age = 5)
assert i7.get_age() == 6
i7.age += 3
assert i7.age == 8
assert i7.name == 'Bot'
i8 = Person3(age = 1, friendIDs = [1,2])
i9 = Person3(age = 1, friendIDs = [1,2])
assert i8 == i9
i8.age = 2
assert i8 > i9


[file driver.py]
import sys

# Dataclasses introduced in 3.7
version = sys.version_info[:2]
if version[0] < 3 or version[1] < 7:
    exit()

# Run the tests in both interpreted and compiled mode
import other
import other_interpreted

# Test for an exceptional cases
from testutil import assertRaises
from native import Person1, Person1b, Person3
from types import BuiltinMethodType

with assertRaises(TypeError, "missing 1 required positional argument"):
    Person1(0)

with assertRaises(TypeError, "missing 2 required positional arguments"):
    Person1b()

with assertRaises(TypeError, "int object expected; got str"):
    Person1('nope', 'test')

p = Person1(0, 'test')
with assertRaises(TypeError, "int object expected; got str"):
    p.age = 'nope'

assert isinstance(Person3().get_age, BuiltinMethodType)

[case testGetAttribute]
class C:
    x: int
    y: int

def getx(c: C) -> int:
    return c.x

def gety(c: C) -> int:
    return c.y
[file driver.py]
from native import C, getx, gety
c = C()
c.x = 10**30
c.y = 10**30 + 1
print(getx(c))
print(gety(c))
[out]
1000000000000000000000000000000
1000000000000000000000000000001

[case testSetAttribute]
class C:
    x: int
    y: int

def setx(c: C, v: int) -> None:
    c.x = v

def sety(c: C, v: int) -> None:
    c.y = v
[file driver.py]
from native import C, setx, sety
c = C()
setx(c, 10**30)
sety(c, 10**30 + 1)
print(c.x)
print(c.y)
setx(c, 4)
sety(c, 5)
print(c.x, c.y)
setx(c, 10**30 + 2)
sety(c, 10**30 + 3)
print(c.x)
print(c.y)
[out]
1000000000000000000000000000000
1000000000000000000000000000001
4 5
1000000000000000000000000000002
1000000000000000000000000000003

[case testAttributeTypes]
from typing import List, Tuple
class C:
    a: List[int]
    b: bool
    c: C
    d: object

def setattrs(o: C, a: List[int], b: bool, c: C) -> None:
    o.a = a
    o.b = b
    o.c = c

def getattrs(o: C) -> Tuple[List[int], bool, C]:
    return o.a, o.b, o.c
[file driver.py]
from native import C, setattrs, getattrs
c1 = C()
c2 = C()
aa = [2]
setattrs(c1, aa, True, c2)
a, b, c = getattrs(c1)
assert a is aa
assert b is True
assert c is c2

o = object()
c1.d = o
assert c1.d is o

[case testConstructClassWithDefaultConstructor]
class C:
    a: int
    b: int

def f() -> C:
    c = C()
    c.a = 13
    return c
[file driver.py]
from native import f, C
c = f()
assert c.a == 13
assert type(c) == C
assert not hasattr(c, 'b')

[case testCastUserClass]
from typing import List

class C:
    x: int

def f(a: List[C]) -> C:
    return a[0]
[file driver.py]
from native import f, C
c = C()
assert f([c]) is c

[case testClass1]
class A:
    def __init__(self, x: int) -> None:
        self.x = x
    def foo(self) -> int:
        return self.x+1
def foo() -> int:
    a = A(20)
    return a.foo()
[file driver.py]
from native import A, foo
a = A(10)
assert a.foo() == 11
assert foo() == 21

[case testGenericClass]
from typing import TypeVar, Generic, Sequence
T = TypeVar('T')
class C(Generic[T]):
    x: T
    def __init__(self, x: T) -> None:
        self.x = x
    def get(self) -> T:
        return self.x
    def set(self, y: T) -> None:
        self.x = y

# Test subclassing generic classes both with and without a generic param
class A(Sequence[int]):
    pass
class B(Sequence[T]):
    pass

def f(c: C[int]) -> int:
    y = c.get()
    d = C[int](2)
    c.set(c.get() + 1 + d.get())
    c.x = c.x + 2
    return c.x

[file driver.py]
from native import C, f
c = C(6)
assert f(c) == 11
c.x = 'x'
assert c.x == 'x'
c.set([1])
assert c.x == [1]
assert c.get() == [1]

[case testSubclass1]
from typing import Tuple

class A:
    def __init__(self) -> None:
        self.x = 10
    def hi(self, suffix: str) -> str:
        return str(self.x) + suffix
class B(A):
    def __init__(self) -> None:
        self.x = 20
        self.y = 'world'
    def hi(self, suffix: str) -> str:
        return 'hello ' + str(self.y) + suffix

def use_a(x: A) -> Tuple[int, str]:
    return (x.x, x.hi(''))
def use_b(x: B) -> str:
    return x.hi('')

[file driver.py]
from native import A, B, use_a, use_b
a = A()
b = B()
assert use_a(a) == (10, '10')
assert use_a(b) == (20, 'hello world')
assert a.x == 10
assert b.x == 20
assert b.y == 'world'
assert a.hi('!') == '10!'
assert b.hi('!') == 'hello world!'
assert use_b(b) == 'hello world'

[case testSubclassSpecialize1]
class A:
    def foo(self, x: int) -> object:
        print('A')
        return str(x)
    def bar(self, x: int) -> None:
        print(x + 1)
class B(A):
    def foo(self, x: object) -> int:
        print('B')
        return id(x)
    def bar(self, x: object) -> None:
        print(x)

def use_a(x: A, y: int) -> object:
    x.bar(10)
    return x.foo(y)

def use_b(x: B, y: object) -> int:
    return x.foo(y)

[file driver.py]
from native import A, B, use_a, use_b
a = A()
b = B()
o = object()
i = 10
assert a.foo(10) == '10'
assert b.foo(o) == id(o)
assert use_a(a, 10) == '10'
assert use_b(b, o) == id(o)
assert use_a(b, i) == id(i)
[out]
A
B
11
A
B
10
B

[case testSubclassSpecialize2]
class A:
    def foo(self, x: int) -> object:
        print('A')
        return str(x)
class B(A):
    def foo(self, x: object) -> object:
        print('B')
        return x
class C(B):
    def foo(self, x: object) -> int:
        print('C')
        return id(x)

def use_a(x: A, y: int) -> object:
    return x.foo(y)

def use_b(x: B, y: object) -> object:
    return x.foo(y)

def use_c(x: C, y: object) -> int:
    return x.foo(y)

[file driver.py]
from native import A, B, C, use_a, use_b, use_c
a = A()
b = B()
c = C()
o = object()
i = 10
assert a.foo(10) == '10'
assert b.foo(o) == o
assert c.foo(o) == id(o)
assert use_a(a, 10) == '10'
assert use_a(b, i) is i
assert use_a(c, i) == id(i)
assert use_b(b, o) == o
assert use_b(c, o) == id(o)
assert use_c(c, o) == id(o)
[out]
A
B
C
A
B
C
B
C
C

[case testIsInstance]
from typing import Optional
class X: pass
class A(X): pass
class B(A): pass

def isa(x: object) -> bool:
    return isinstance(x, A)
def isint(x: object) -> bool:
    return isinstance(x, int)
def isstr(x: object) -> bool:
    return isinstance(x, str)
def islist(x: object) -> bool:
    return isinstance(x, list)
def ist(x: object, t: object) -> bool:  # TODO: Second argument should be 'type'
    return isinstance(x, t)
def pointless(x: Optional[X]) -> str:
    if isinstance(x, A):
        return str(x)
    return ''
[file driver.py]
from native import X, A, B, isa, isint, isstr, islist, ist
assert isa(1) == False
assert isa(A()) == True
assert isa(B()) == True
assert isa(X()) == False

assert isint(1) == True
assert isint('') == False
assert isint(A()) == False

assert isstr(1) == False
assert isstr('') == True

assert islist(1) == False
assert islist([]) == True

assert ist(1, int) == True
assert ist(1, str) == False
try:
    ist(1, 2)
except TypeError:
    pass
else:
    assert False

[case testSubclassUninitAttr]
class X:
    x: int
class A(X):
    pass
[file driver.py]
import traceback
from native import A
try:
    A().x
except AttributeError:
    traceback.print_exc()
[out]
Traceback (most recent call last):
  File "driver.py", line 4, in <module>
    A().x
AttributeError: attribute 'x' of 'X' undefined

[case testClassMethods]
MYPY = False
if MYPY:
    from typing import ClassVar
class C:
    lurr: 'ClassVar[int]' = 9
    @staticmethod
    def foo(x: int) -> int: return 10 + x
    @classmethod
    def bar(cls, x: int) -> int: return cls.lurr + x
    @staticmethod
    def baz(x: int, y: int = 10) -> int: return y - x
    @classmethod
    def quux(cls, x: int, y: int = 10) -> int: return y - x

class D(C):
    def f(self) -> int:
        return super().foo(1) + super().bar(2) + super().baz(10) + super().quux(10)

def test1() -> int:
    return C.foo(1) + C.bar(2) + C.baz(10) + C.quux(10) + C.quux(y=10, x=9)
def test2() -> int:
    c = C()
    return c.foo(1) + c.bar(2) + c.baz(10)
[file driver.py]
from native import *
assert C.foo(10) == 20
assert C.bar(10) == 19
c = C()
assert c.foo(10) == 20
assert c.bar(10) == 19

assert test1() == 23
assert test2() == 22

d = D()
assert d.f() == 22

[case testSuper]
from mypy_extensions import trait
from typing import List

class A:
    def __init__(self, x: int) -> None:
        self.x = x

    def foo(self, x: int) -> int:
        return x

class B(A):
    def __init__(self, x: int, y: int) -> None:
        super().__init__(x)
        self.y = y

    def foo(self, x: int) -> int:
        return super().foo(x+1)

class C(B):
    def __init__(self, x: int, y: int) -> None:
        init = super(C, self).__init__
        init(x, y+1)

    def foo(self, x: int) -> int:
        # should go to A, not B
        return super(B, self).foo(x+1)

class X:
    def __init__(self, x: int) -> None:
        self.x = x
class Y(X):
    pass
class Z(Y):
    def __init__(self, x: int, y: int) -> None:
        super().__init__(x)
        self.y = y

@trait
class T:
    def v_int(self, x: int) -> None: pass
    def v_list(self, x: List[int]) -> None:
        if x:
            self.v_int(x[0])
            self.v_list(x[1:])

class PrintList(T):
    def v_int(self, x: int) -> None:
        print(x)
    def v_list(self, x: List[int]) -> None:
        print('yo!')
        super().v_list(x)

[file driver.py]
import traceback
from native import *
b = B(10, 20)
assert b.x == 10 and b.y == 20
c = C(10, 20)
assert c.x == 10 and c.y == 21
z = Z(10, 20)
assert z.x == 10 and z.y == 20

assert c.foo(10) == 11

PrintList().v_list([1,2,3])
[out]
yo!
1
yo!
2
yo!
3
yo!

[case testSubclassException]
class Failure(Exception):
    def __init__(self, x: int) -> None:
        self.x = x

def foo() -> None:
    raise Failure(10)

def heyo() -> int:
    try:
        foo()
    except Failure as e:
        return e.x
    return -1

[file driver.py]
from native import foo, heyo, Failure
try:
    foo()
except Failure as e:
    assert str(e) == '10'
    assert e.x == 10
heyo()

[case testSubclassDict]
from typing import Dict
class WelpDict(Dict[str, int]):
    def __init__(self) -> None:
        self.emarhavil = 3
    def foo(self) -> int:
        return self.emarhavil

def welp() -> int:
    x = WelpDict()
    x['a'] = 10
    x['b'] = 15
    x.emarhavil = 5
    return x['a'] + x['b'] + x.emarhavil + x.foo()

[file driver.py]
from native import welp
assert welp() == 35

[case testSubclassPy]
from b import B, V
class A(B):
    def __init__(self, x: int, y: int) -> None:
        super().__init__(y)
        self.x = x

    def foo(self, x: int) -> int:
        print("hi", x)
        return x+1

class C(V[int]):
    def f(self) -> int: return 10

assert isinstance(C(), V)

def f(x: A) -> None:
    print(x.x)
    print(x.y)
    print(x.foo(20))

[file b.py]
from typing import Generic, TypeVar
T = TypeVar('T')

class B:
    def __init__(self, y: int) -> None:
        self.y = y
    def foo(self, x: int) -> int:
        print("parent!")
        return x + self.y
    def bar(self) -> None:
        print("hello!", self.y)

class V(Generic[T]):
    def f(self) -> T:
        raise Exception('unimplemented')
[file driver.py]
import native
a = native.A(10, 20)
a.foo(10)
a.bar()
native.f(a)
[out]
hi 10
hello! 20
10
20
hi 20
21

[case testDisallowSubclassFromPy]
# We'll want to allow this at some point but right now we need to
# disallow it because it doesn't work.
class A:
    pass
[file b.py]
from native import A

# It would be better if we disallowed it at class decl time but it is
# really easy to do in __new__

class B(A):
    pass

[file driver.py]
from b import B
try:
    B()
except TypeError:
    pass
else:
    assert False, "instantiating was supposed to fail"

[case testClassVariable]
MYPY = False
if MYPY:
    from typing import ClassVar
class A:
    x = 10  # type: ClassVar[int]

def g(x: int) -> None:
    A.x = 10
def f() -> int:
    return A.x
[file driver.py]
from native import A, f
assert f() == 10
A.x = 200
assert f() == 200

[case testDefaultVars]
from typing import Optional
class A:
    x = 10
    w: object = 10
    def lol(self) -> None:
        self.x = 100

LOL = 'lol'
class B(A):
    y = LOL
    z = None  # type: Optional[str]
    b = True
    bogus = None  # type: int

def g() -> None:
    a = A()
    assert a.x == 10
    a.x = 20
    assert a.x == 20
    b = B()
    assert b.x == 10
    b.x = 20
    assert b.x == 20
    assert b.y == 'lol'
    b.y = 'rofl'
    assert b.y == 'rofl'
    assert b.z is None
[file driver.py]
from native import *
g()

a = A()
assert a.x == 10
a.x = 20
assert a.x == 20
b = B()
assert b.x == 10
b.x = 20
assert b.x == 20
assert b.y == 'lol'
b.y = 'rofl'
assert b.y == 'rofl'
assert b.z is None
# N.B: this doesn't match cpython
assert not hasattr(b, 'bogus')

[case testProtocol]
from typing_extensions import Protocol

class Proto(Protocol):
    def foo(self, x: int) -> None:
        pass

    def bar(self, x: int) -> None:
        pass

class A:
    def foo(self, x: int) -> None:
        print("A:", x)

    def bar(self, *args: int, **kwargs: int) -> None:
        print("A:", args, kwargs)

class B(A, Proto):
    def foo(self, x: int) -> None:
        print("B:", x)

    def bar(self, *args: int, **kwargs: int) -> None:
        print("B:", args, kwargs)

def f(x: Proto) -> None:
    x.foo(20)
    x.bar(x=20)

[file driver.py]
from native import A, B, f

f(A())
f(B())

# ... this exploits a bug in glue methods to distinguish whether we
# are making a direct call or a pycall...
[out]
A: 20
A: () {'x': 20}
B: 20
B: (20,) {}

[case testMethodOverrideDefault1]
class A:
    def foo(self, x: int) -> None:
        pass
class B(A):
    def foo(self, x: int, y: int = 10) -> None:
        print(x, y)

def a(x: A) -> None:
    x.foo(1)
def b(x: B) -> None:
    x.foo(2)
    x.foo(2, 3)

[file driver.py]
from native import B, a, b
a(B())
b(B())
[out]
1 10
2 10
2 3

[case testMethodOverrideDefault2]
class A:
    def foo(self, *, x: int = 0) -> None:
        pass
    def bar(self, *, x: int = 0, y: int = 0) -> None:
        pass
    def baz(self, x: int = 0) -> None:
        pass
class B(A):
    def foo(self, *, y: int = 0, x: int = 0) -> None:
        print(x, y)
    def bar(self, *, y: int = 0, x: int = 0) -> None:
        print(x, y)
    def baz(self, x: int = 0, *, y: int = 0) -> None:
        print(x, y)

def a(x: A) -> None:
    x.foo(x=1)
    x.bar(x=1, y=2)
    x.bar(x=2, y=1)
    x.baz()
    x.baz(1)
    x.baz(x=2)

[file driver.py]
from native import B, a
a(B())
[out]
1 0
1 2
2 1
0 0
1 0
2 0

[case testMethodOverrideDefault3]
class A:
    @classmethod
    def foo(cls, *, x: int = 0) -> None:
        pass
    @staticmethod
    def bar(*, x: int = 0) -> None:
        pass
    @staticmethod
    def baz() -> object:
        pass
class B(A):
    @classmethod
    def foo(cls, *, y: int = 0, x: int = 0) -> None:
        print(x, y)
        print(cls.__name__)  # type: ignore
    @staticmethod
    def bar(*, y: int = 0, x: int = 0) -> None:
        print(x, y)
    @staticmethod
    def baz() -> int:
        return 10

# This is just to make sure that this stuff works even when the
# methods might be overridden.
class C(B):
    @classmethod
    def foo(cls, *, y: int = 0, x: int = 0) -> None:
        pass
    @staticmethod
    def bar(*, y: int = 0, x: int = 0) -> None:
        pass
    @staticmethod
    def baz() -> int:
        return 10

def a(x: A) -> None:
    x.foo(x=1)
    x.bar(x=1)
    print(x.baz())

[file driver.py]
from native import B, a
a(B())
[out]
1 0
B
1 0
10

[case testOverride]
class A:
    def f(self) -> int:
        return 0
    def g(self) -> int:
        return 1

class B(A):
    def g(self) -> int:
        return 2

class C(B):
    def f(self) -> int:
        return 3

def test() -> None:
    ba: A = B()
    ca: A = C()
    assert ba.f() == 0
    assert ba.g() == 2
    assert ca.f() == 3
    assert ca.g() == 2
    cc = C()
    assert cc.f() == 3
    assert cc.g() == 2
    print('ok')
[file driver.py]
import native
native.test()
[out]
ok

[case testNoMetaclass]
from foo import Base

class Nothing(Base):  # type: ignore
    pass

[file foo.py]
from typing import Any
class Meta(type):
    pass

class _Base(metaclass=Meta):
    pass
Base = _Base  # type: Any

[file driver.py]
try:
    import native
except TypeError as e:
    assert(str(e) == "mypyc classes can't have a metaclass")

[case testMetaclass]
from meta import Meta
import six

class Nothing1(metaclass=Meta):
    pass

def ident(x): return x

@ident
class Test:
    pass

class Nothing2(six.with_metaclass(Meta, Test)):
    pass

@six.add_metaclass(Meta)
class Nothing3:
    pass

[file meta.py]
from typing import Any
class Meta(type):
    def __new__(mcs, name, bases, dct):
        dct['X'] = 10
        return super().__new__(mcs, name, bases, dct)


[file driver.py]
from native import Nothing1, Nothing2, Nothing3
assert Nothing1.X == 10
assert Nothing2.X == 10
assert Nothing3.X == 10

[case testPickling]
from mypy_extensions import trait
from typing import Any, TypeVar, Generic

def dec(x: Any) -> Any:
    return x

class A:
    x: int
    y: str

class B(A):
    z: bool

    def __init__(self, x: int, y: str, z: bool) -> None:
        self.x = x
        self.y = y
        self.z = z

@trait
class T:
    a: str

class C(B, T):
    w: object

    # property shouldn't go in
    @property
    def foo(self) -> int:
        return 0

@dec
class D:
    x: int

class E(D):
    y: int


U = TypeVar('U')

class F(Generic[U]):
    y: int

class G(F[int]):
    pass

[file driver.py]
from native import A, B, T, C, D, E, F, G

import copy
import pickle

assert A.__mypyc_attrs__ == ('x', 'y')
assert B.__mypyc_attrs__ == ('z', 'x', 'y')
assert T.__mypyc_attrs__ == ('a',)
assert C.__mypyc_attrs__ == ('w', 'z', 'x', 'y', 'a')
assert not hasattr(D, '__mypyc_attrs__')
assert E.__mypyc_attrs__ == ('y', '__dict__')
assert F.__mypyc_attrs__ == ('y', '__dict__')
assert G.__mypyc_attrs__ == ('y', '__dict__')

b = B(10, '20', False)
assert b.__getstate__() == {'z': False, 'x': 10, 'y': '20'}
b2 = copy.copy(b)
assert b is not b2 and b.y == b2.y

b3 = pickle.loads(pickle.dumps(b))
assert b is not b3 and b.y == b3.y

e = E()
e.x = 10
e.y = 20

assert e.__getstate__() == {'y': 20, '__dict__': {'x': 10}}
e2 = pickle.loads(pickle.dumps(e))
assert e is not e2 and e.x == e2.x and e.y == e2.y


[case testInterpretedParentInit]
from interp import C
from typing import TypeVar

T = TypeVar('T')

def dec(x: T) -> T:
    return x

@dec
class A:
    def __init__(self, x: int) -> None:
        self.x = x

class B(A):
    s = 'test'

def b(x: int) -> B: return B(x)

class D(C):
    s = 'test'

def d(x: int) -> D: return D(x)


[file interp.py]
class C:
    def __init__(self, x: int) -> None:
        self.x = x

[file driver.py]
from native import b, d, B, D

def test(f, v):
    x = f(v)
    assert x.x == v
    assert x.s == 'test'

test(b, 20)
test(d, 30)
test(B, -1)
test(D, -2)

[case testInterpretedInherit]
from typing import TypeVar, Any, overload
from mypy_extensions import mypyc_attr, trait

T = TypeVar('T')
def dec(x: T) -> T: return x

@mypyc_attr(allow_interpreted_subclasses=True)
class Top:
    def spam(self) -> str:
        return "grandparent"

@mypyc_attr(allow_interpreted_subclasses=True)
@trait
class Trait:
    def trait_method(self) -> str:
        return "trait"

@mypyc_attr(allow_interpreted_subclasses=True)
class Foo(Top, Trait):
    def __init__(self, x: int) -> None:
        self.x = x

    def foo(self) -> str:
        return "parent foo: " + self.bar(self.x)

    def bar(self, x: int) -> str:
        return "parent bar: {}".format(x + self.x)

    @dec
    def decorated(self) -> str:
        return "decorated parent"

    @property
    def read_property(self) -> str:
        return "parent prop"

    @overload
    def overloaded(self, index: int) -> int: ...

    @overload
    def overloaded(self, index: str) -> str: ...

    def overloaded(self, index: Any) -> Any:
        return index

def foo(x: Foo) -> str:
    return x.foo()

def bar(x: Foo, y: int) -> str:
    return x.bar(y)

def spam(x: Top) -> str:
    return x.spam()

def decorated(x: Foo) -> str:
    return x.decorated()

def prop(x: Foo) -> str:
    return x.read_property

def trait_method(x: Trait) -> str:
    return x.trait_method()

def overloaded(x: Foo, s: str) -> str:
    return x.overloaded(s)

[file interp.py]
from typing import Any
from native import Foo

class Bar(Foo):
    def bar(self, x: int) -> str:
        return "child bar: {}".format(x + self.x)

    def spam(self) -> str:
        assert super().spam() == "grandparent"
        return "child"

    @property
    def read_property(self) -> str:
        return "child prop"

    def decorated(self) -> str:
        return "decorated child"

    def trait_method(self) -> str:
        return "child"

    def overloaded(self, index: Any) -> Any:
        return index + index


class InterpBase:
    def eggs(self) -> str:
        return "eggs"

class Baz(InterpBase, Bar):
    def __init__(self) -> None:
        super().__init__(1000)
        self.z = self.read_property

[file driver.py]
from native import Foo, foo, bar, spam, decorated, overloaded, prop, trait_method
from interp import Bar, Baz
from unittest.mock import patch
from testutil import assertRaises

x = Foo(10)
y = Bar(20)
z = Baz()

assert isinstance(y, Bar)
assert y.x == 20
assert y.bar(10) == "child bar: 30"
assert y.foo() == "parent foo: child bar: 40"
assert foo(y) == "parent foo: child bar: 40"
assert bar(y, 30) == "child bar: 50"
y.x = 30
assert bar(y, 30) == "child bar: 60"

assert spam(y) == "child"
assert y.read_property == "child prop"
assert prop(x) == "parent prop"
assert prop(y) == "child prop"
assert y.decorated() == "decorated child"
assert decorated(y) == "decorated child"
assert y.overloaded("test") == "testtest"
assert overloaded(y, "test") == "testtest"

assert y.trait_method() == "child"
assert trait_method(y) == "child"

assert z.bar(10) == "child bar: 1010"
assert bar(z, 10) == "child bar: 1010"
assert z.z == "child prop"
assert z.eggs() == "eggs"

with patch("interp.Bar.spam", lambda self: "monkey patched"):
    assert y.spam() == "monkey patched"
    spam(y) == "monkey patched"

with patch("interp.Bar.spam", lambda self: 20):
    assert y.spam() == 20
    with assertRaises(TypeError, "str object expected; got int"):
        spam(y)

with assertRaises(TypeError, "int object expected; got str"):
    y.x = "test"

[case testProperty]
from typing import Callable
from mypy_extensions import trait
class Temperature:
    @property
    def celsius(self) -> float:
        return 5.0 * (self.farenheit - 32.0) / 9.0

    def __init__(self, farenheit: float) -> None:
        self.farenheit = farenheit

    def print_temp(self) -> None:
        print("F:", self.farenheit, "C:", self.celsius)

    @property
    def rankine(self) -> float:
        raise NotImplementedError

class Access:
    @property
    def number_of_accesses(self) -> int:
        self._count += 1
        return self._count
    def __init__(self) -> None:
        self._count = 0

from typing import Callable
class BaseProperty:
    @property
    def doc(self) -> str:
        return "Represents a sequence of values. Updates itself by next, which is a new value."

    @property
    def value(self) -> object:
        return self._incrementer

    @property
    def bad_value(self) -> object:
        return self._incrementer

    @property
    def next(self) -> BaseProperty:
        return BaseProperty(self._incrementer + 1)

    def __init__(self, value: int) -> None:
        self._incrementer = value

class DerivedProperty(BaseProperty):
    @property
    def value(self) -> int:
        return self._incrementer

    @property
    def bad_value(self) -> object:
        return self._incrementer

    def __init__(self, incr_func: Callable[[int], int], value: int) -> None:
        BaseProperty.__init__(self, value)
        self._incr_func = incr_func

    @property
    def next(self) -> DerivedProperty:
        return DerivedProperty(self._incr_func, self._incr_func(self.value))

class AgainProperty(DerivedProperty):
    @property
    def next(self) -> AgainProperty:
        return AgainProperty(self._incr_func, self._incr_func(self._incr_func(self.value)))

    @property
    def bad_value(self) -> int:
        return self._incrementer

def print_first_n(n: int, thing: BaseProperty) -> None:
    vals = []
    cur_thing = thing
    for _ in range(n):
        vals.append(cur_thing.value)
        cur_thing = cur_thing.next
    print ('', vals)

@trait
class Trait:
    @property
    def value(self) -> int:
        return 3

class Printer(Trait):
    def print_value(self) -> None:
        print(self.value)

[file driver.py]
from native import Temperature, Access
import traceback
x = Temperature(32.0)
try:
    print (x.rankine)
except NotImplementedError as e:
    traceback.print_exc()
print (x.celsius)
x.print_temp()

y = Temperature(212.0)
print (y.celsius)
y.print_temp()

z = Access()
print (z.number_of_accesses)
print (z.number_of_accesses)
print (z.number_of_accesses)
print (z.number_of_accesses)

from native import BaseProperty, DerivedProperty, AgainProperty, print_first_n
a = BaseProperty(7)
b = DerivedProperty((lambda x: x // 2 if (x % 2 == 0) else 3 * x + 1), 7)
c = AgainProperty((lambda x: x // 2 if (x % 2 == 0) else 3 * x + 1), 7)

def py_print_first_n(n: int, thing: BaseProperty) -> None:
    vals = []
    cur_thing = thing
    for _ in range(n):
        vals.append(cur_thing.value)
        cur_thing = cur_thing.next
    print ('', vals)

py_print_first_n(20, a)
py_print_first_n(20, b)
py_print_first_n(20, c)

print(a.next.next.next.bad_value)
print(b.next.next.next.bad_value)
print(c.next.next.next.bad_value)

print_first_n(20, a)
print_first_n(20, b)
print_first_n(20, c)

print (a.doc)
print (b.doc)
print (c.doc)

from native import Printer
Printer().print_value()
print (Printer().value)
[out]
Traceback (most recent call last):
  File "driver.py", line 5, in <module>
    print (x.rankine)
  File "native.py", line 16, in rankine
    raise NotImplementedError
NotImplementedError
0.0
F: 32.0 C: 0.0
100.0
F: 212.0 C: 100.0
1
2
3
4
 [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
 [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1]
 [7, 11, 17, 26, 40, 10, 16, 4, 1, 2, 4, 1, 2, 4, 1, 2, 4, 1, 2, 4]
10
34
26
 [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
 [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1]
 [7, 11, 17, 26, 40, 10, 16, 4, 1, 2, 4, 1, 2, 4, 1, 2, 4, 1, 2, 4]
Represents a sequence of values. Updates itself by next, which is a new value.
Represents a sequence of values. Updates itself by next, which is a new value.
Represents a sequence of values. Updates itself by next, which is a new value.
3
3

[case testPropertySetters]

from mypy_extensions import trait

class Foo():
    def __init__(self) -> None:
        self.attr = "unmodified"

class A:
    def __init__(self) -> None:
        self._x = 0
        self._foo = Foo()

    @property
    def x(self) -> int:
        return self._x

    @x.setter
    def x(self, val : int) -> None:
        self._x = val

    @property
    def foo(self) -> Foo:
        return self._foo

    @foo.setter
    def foo(self, val : Foo) -> None:
        self._foo = val

# Overrides base property setters and getters
class B(A):
    def __init__(self) -> None:
        self._x = 10

    @property
    def x(self) -> int:
        return self._x + 1

    @x.setter
    def x(self, val : int) -> None:
        self._x = val + 1

#Inerits base property setters and getters
class C(A):
    def __init__(self) -> None:
        A.__init__(self)

@trait
class D():
    def __init__(self) -> None:
        self._x = 0

    @property
    def x(self) -> int:
        return self._x

    @x.setter
    def x(self, val : int) -> None:
        self._x = val

#Inherits trait property setters and getters
class E(D):
    def __init__(self) -> None:
        D.__init__(self)

#Overrides trait property setters and getters
class F(D):
    def __init__(self) -> None:
        self._x = 10

    @property
    def x(self) -> int:
        return self._x + 10

    @x.setter
    def x(self, val : int) -> None:
        self._x = val + 10

# # Property setter and getter are subtypes of base property setters and getters
# # class G(A):
# #   def __init__(self) -> None:
# #       A.__init__(self)

# #   @property
# #   def y(self) -> int:
# #       return self._y

# #   @y.setter
# #   def y(self, val : object) -> None:
# #       self._y = val

[file other.py]
# Run in both interpreted and compiled mode

from native import A, B, C, D, E, F

a = A()
assert a.x == 0
assert a._x == 0
a.x = 1
assert a.x == 1
assert a._x == 1
a._x = 0
assert a.x == 0
assert a._x == 0
b = B()
assert b.x == 11
assert b._x == 10
b.x = 11
assert b.x == 13
b._x = 11
assert b.x == 12
c = C()
assert c.x == 0
c.x = 1000
assert c.x == 1000
e = E()
assert e.x == 0
e.x = 1000
assert e.x == 1000
f = F()
assert f.x == 20
f.x = 30
assert f.x == 50

[file driver.py]
# Run the tests in both interpreted and compiled mode
import other
import other_interpreted

[out]

[case testSubclassAttributeAccess]
from mypy_extensions import trait

class A:
    v = 0

class B(A):
    v = 1

class C(B):
    v = 2

[file driver.py]
from native import A, B, C

a = A()
b = B()
c = C()