Skip to content

Commit 985ce43

Browse files
committed
add a bunch of tests
1 parent 632af66 commit 985ce43

File tree

1 file changed

+76
-8
lines changed

1 file changed

+76
-8
lines changed

mypyc/test-data/run-classes.test

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,8 +1218,12 @@ test(B, -1)
12181218
test(D, -2)
12191219

12201220
[case testInterpretedInherit]
1221+
from typing import TypeVar, Any, overload
12211222
from mypy_extensions import mypyc_attr, trait
12221223

1224+
T = TypeVar('T')
1225+
def dec(x: T) -> T: return x
1226+
12231227
@mypyc_attr(allow_interpreted_subclasses=True)
12241228
class Top:
12251229
def spam(self) -> str:
@@ -1234,18 +1238,31 @@ class Trait:
12341238
@mypyc_attr(allow_interpreted_subclasses=True)
12351239
class Foo(Top, Trait):
12361240
def __init__(self, x: int) -> None:
1237-
self.x = 10
1241+
self.x = x
12381242

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

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

1249+
@dec
1250+
def decorated(self) -> str:
1251+
return "decorated parent"
1252+
12451253
@property
12461254
def read_property(self) -> str:
12471255
return "parent prop"
12481256

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+
12491266
def foo(x: Foo) -> str:
12501267
return x.foo()
12511268

@@ -1255,46 +1272,97 @@ def bar(x: Foo, y: int) -> str:
12551272
def spam(x: Top) -> str:
12561273
return x.spam()
12571274

1275+
def decorated(x: Foo) -> str:
1276+
return x.decorated()
1277+
12581278
def prop(x: Foo) -> str:
12591279
return x.read_property
12601280

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

1284+
def overloaded(x: Foo, s: str) -> str:
1285+
return x.overloaded(s)
1286+
12641287
[file interp.py]
1288+
from typing import Any
12651289
from native import Foo
12661290

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

12711295
def spam(self) -> str:
1296+
assert super().spam() == "grandparent"
12721297
return "child"
12731298

12741299
@property
12751300
def read_property(self) -> str:
12761301
return "child prop"
12771302

1303+
def decorated(self) -> str:
1304+
return "decorated child"
1305+
12781306
def trait_method(self) -> str:
12791307
return "child"
12801308

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+
12811322
[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
12841327

12851328
x = Foo(10)
12861329
y = Bar(20)
1330+
z = Baz()
12871331

12881332
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+
12941341
assert spam(y) == "child"
12951342
assert y.read_property == "child prop"
12961343
assert prop(x) == "parent prop"
12971344
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"
12981349

12991350
assert y.trait_method() == "child"
13001351
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy