@@ -1218,8 +1218,12 @@ test(B, -1)
1218
1218
test(D, -2)
1219
1219
1220
1220
[case testInterpretedInherit]
1221
+ from typing import TypeVar, Any, overload
1221
1222
from mypy_extensions import mypyc_attr, trait
1222
1223
1224
+ T = TypeVar('T')
1225
+ def dec(x: T) -> T: return x
1226
+
1223
1227
@mypyc_attr(allow_interpreted_subclasses=True)
1224
1228
class Top:
1225
1229
def spam(self) -> str:
@@ -1234,18 +1238,31 @@ class Trait:
1234
1238
@mypyc_attr(allow_interpreted_subclasses=True)
1235
1239
class Foo(Top, Trait):
1236
1240
def __init__(self, x: int) -> None:
1237
- self.x = 10
1241
+ self.x = x
1238
1242
1239
1243
def foo(self) -> str:
1240
1244
return "parent foo: " + self.bar(self.x)
1241
1245
1242
1246
def bar(self, x: int) -> str:
1243
1247
return "parent bar: {}".format(x + self.x)
1244
1248
1249
+ @dec
1250
+ def decorated(self) -> str:
1251
+ return "decorated parent"
1252
+
1245
1253
@property
1246
1254
def read_property(self) -> str:
1247
1255
return "parent prop"
1248
1256
1257
+ @overload
1258
+ def overloaded(self, index: int) -> int: ...
1259
+
1260
+ @overload
1261
+ def overloaded(self, index: str) -> str: ...
1262
+
1263
+ def overloaded(self, index: Any) -> Any:
1264
+ return index
1265
+
1249
1266
def foo(x: Foo) -> str:
1250
1267
return x.foo()
1251
1268
@@ -1255,46 +1272,97 @@ def bar(x: Foo, y: int) -> str:
1255
1272
def spam(x: Top) -> str:
1256
1273
return x.spam()
1257
1274
1275
+ def decorated(x: Foo) -> str:
1276
+ return x.decorated()
1277
+
1258
1278
def prop(x: Foo) -> str:
1259
1279
return x.read_property
1260
1280
1261
1281
def trait_method(x: Trait) -> str:
1262
1282
return x.trait_method()
1263
1283
1284
+ def overloaded(x: Foo, s: str) -> str:
1285
+ return x.overloaded(s)
1286
+
1264
1287
[file interp.py]
1288
+ from typing import Any
1265
1289
from native import Foo
1266
1290
1267
1291
class Bar(Foo):
1268
1292
def bar(self, x: int) -> str:
1269
1293
return "child bar: {}".format(x + self.x)
1270
1294
1271
1295
def spam(self) -> str:
1296
+ assert super().spam() == "grandparent"
1272
1297
return "child"
1273
1298
1274
1299
@property
1275
1300
def read_property(self) -> str:
1276
1301
return "child prop"
1277
1302
1303
+ def decorated(self) -> str:
1304
+ return "decorated child"
1305
+
1278
1306
def trait_method(self) -> str:
1279
1307
return "child"
1280
1308
1309
+ def overloaded(self, index: Any) -> Any:
1310
+ return index + index
1311
+
1312
+
1313
+ class InterpBase:
1314
+ def eggs(self) -> str:
1315
+ return "eggs"
1316
+
1317
+ class Baz(InterpBase, Bar):
1318
+ def __init__(self) -> None:
1319
+ super().__init__(1000)
1320
+ self.z = self.read_property
1321
+
1281
1322
[file driver.py]
1282
- from native import Foo, foo, bar, spam, prop, trait_method
1283
- from interp import Bar
1323
+ from native import Foo, foo, bar, spam, decorated, overloaded, prop, trait_method
1324
+ from interp import Bar, Baz
1325
+ from unittest.mock import patch
1326
+ from testutil import assertRaises
1284
1327
1285
1328
x = Foo(10)
1286
1329
y = Bar(20)
1330
+ z = Baz()
1287
1331
1288
1332
assert isinstance(y, Bar)
1289
- assert y.x == 10
1290
- assert y.bar(10) == "child bar: 20"
1291
- assert y.foo() == "parent foo: child bar: 20"
1292
- assert foo(y) == "parent foo: child bar: 20"
1293
- assert bar(y, 30) == "child bar: 40"
1333
+ assert y.x == 20
1334
+ assert y.bar(10) == "child bar: 30"
1335
+ assert y.foo() == "parent foo: child bar: 40"
1336
+ assert foo(y) == "parent foo: child bar: 40"
1337
+ assert bar(y, 30) == "child bar: 50"
1338
+ y.x = 30
1339
+ assert bar(y, 30) == "child bar: 60"
1340
+
1294
1341
assert spam(y) == "child"
1295
1342
assert y.read_property == "child prop"
1296
1343
assert prop(x) == "parent prop"
1297
1344
assert prop(y) == "child prop"
1345
+ assert y.decorated() == "decorated child"
1346
+ assert decorated(y) == "decorated child"
1347
+ assert y.overloaded("test") == "testtest"
1348
+ assert overloaded(y, "test") == "testtest"
1298
1349
1299
1350
assert y.trait_method() == "child"
1300
1351
assert trait_method(y) == "child"
1352
+
1353
+ assert z.bar(10) == "child bar: 1010"
1354
+ assert bar(z, 10) == "child bar: 1010"
1355
+ assert z.z == "child prop"
1356
+ assert z.eggs() == "eggs"
1357
+
1358
+ with patch("interp.Bar.spam", lambda self: "monkey patched"):
1359
+ assert y.spam() == "monkey patched"
1360
+ spam(y) == "monkey patched"
1361
+
1362
+ with patch("interp.Bar.spam", lambda self: 20):
1363
+ assert y.spam() == 20
1364
+ with assertRaises(TypeError, "str object expected; got int"):
1365
+ spam(y)
1366
+
1367
+ with assertRaises(TypeError, "int object expected; got str"):
1368
+ y.x = "test"
0 commit comments