11# Test cases for weakrefs (compile and run)
22
33[case testWeakrefRef]
4- import pytest # type: ignore [import-not-found]
5- from weakref import proxy, ref
4+ from weakref import ref
65from mypy_extensions import mypyc_attr
76
87@mypyc_attr(native_class=False)
98class Object:
109 """some random weakreffable object"""
11- def some_meth(self) -> int:
12- return 1
1310
1411def test_weakref_ref():
1512 obj = Object()
@@ -18,38 +15,80 @@ def test_weakref_ref():
1815 obj = None
1916 assert r() is None, r()
2017
18+ [file driver.py]
19+ from native import test_weakref_ref
20+
21+ test_weakref_ref()
22+
23+
24+ [case testWeakrefRefWithCallback]
25+ from weakref import ref
26+ from mypy_extensions import mypyc_attr
27+
28+ @mypyc_attr(native_class=False)
29+ class Object:
30+ """some random weakreffable object"""
31+
2132def test_weakref_ref_with_callback():
2233 obj = Object()
2334 r = ref(obj, lambda x: x)
2435 assert r() is obj
2536 obj = None
2637 assert r() is None, r()
2738
39+ [file driver.py]
40+ from native import test_weakref_ref_with_callback
41+
42+ test_weakref_ref_with_callback()
43+
44+
45+ [case testWeakrefProxy]
46+ import pytest # type: ignore [import-not-found]
47+ from weakref import proxy
48+ from mypy_extensions import mypyc_attr
49+
50+ @mypyc_attr(native_class=False)
51+ class Object:
52+ """some random weakreffable object"""
53+ def some_meth(self) -> int:
54+ return 1
55+
2856def test_weakref_proxy():
2957 obj = Object()
3058 p = proxy(obj)
59+ assert obj.some_meth() == 1
3160 assert p.some_meth() == 1
3261 obj = None
3362 with pytest.raises(ReferenceError):
3463 p.some_meth()
3564
65+ [file driver.py]
66+ from native import test_weakref_proxy
67+
68+ test_weakref_proxy()
69+
70+
71+ [case testWeakrefProxyWithCallback]
72+ import pytest # type: ignore [import-not-found]
73+ from weakref import proxy
74+ from mypy_extensions import mypyc_attr
75+
76+ @mypyc_attr(native_class=False)
77+ class Object:
78+ """some random weakreffable object"""
79+ def some_meth(self) -> int:
80+ return 1
81+
3682def test_weakref_proxy_with_callback():
3783 obj = Object()
3884 p = proxy(obj, lambda x: x)
85+ assert obj.some_meth() == 1
3986 assert p.some_meth() == 1
4087 obj = None
4188 with pytest.raises(ReferenceError):
4289 p.some_meth()
4390
4491[file driver.py]
45- from native import (
46- test_weakref_ref,
47- test_weakref_ref_with_callback,
48- test_weakref_proxy,
49- test_weakref_proxy_with_callback,
50- )
92+ from native import test_weakref_proxy_with_callback
5193
52- test_weakref_ref()
53- test_weakref_ref_with_callback()
54- test_weakref_proxy()
5594test_weakref_proxy_with_callback()
0 commit comments