From d29eab8717564ae51969f3b7760f8bf5c16919d4 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Mon, 4 Aug 2025 01:59:22 +0000 Subject: [PATCH 01/24] add tests --- mypyc/test-data/run-weakref.test | 70 ++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index 902c9e407ff45..892c29c50521d 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -7,7 +7,6 @@ from mypy_extensions import mypyc_attr @mypyc_attr(native_class=False) class Object: """some random weakreffable object""" - pass def test_weakref_ref(): obj = Object() @@ -16,6 +15,20 @@ def test_weakref_ref(): obj = None assert r() is None, r() +[file driver.py] +from native import test_weakref_ref + +test_weakref_ref() + + +[case testWeakrefRefWithCallback] +from weakref import ref +from mypy_extensions import mypyc_attr + +@mypyc_attr(native_class=False) +class Object: + """some random weakreffable object""" + def test_weakref_ref_with_callback(): obj = Object() r = ref(obj, lambda x: x) @@ -24,7 +37,58 @@ def test_weakref_ref_with_callback(): assert r() is None, r() [file driver.py] -from native import test_weakref_ref, test_weakref_ref_with_callback +from native import test_weakref_ref_with_callback -test_weakref_ref() test_weakref_ref_with_callback() + + +[case testWeakrefProxy] +import pytest # type: ignore [import-not-found] +from weakref import proxy +from mypy_extensions import mypyc_attr + +@mypyc_attr(native_class=False) +class Object: + """some random weakreffable object""" + def some_meth(self) -> int: + return 1 + +def test_weakref_proxy(): + obj = Object() + p = proxy(obj) + assert obj.some_meth() == 1 + assert p.some_meth() == 1 + obj = None + with pytest.raises(ReferenceError): + p.some_meth() + +[file driver.py] +from native import test_weakref_proxy + +test_weakref_proxy() + + +[case testWeakrefProxyWithCallback] +import pytest # type: ignore [import-not-found] +from weakref import proxy +from mypy_extensions import mypyc_attr + +@mypyc_attr(native_class=False) +class Object: + """some random weakreffable object""" + def some_meth(self) -> int: + return 1 + +def test_weakref_proxy_with_callback(): + obj = Object() + p = proxy(obj, lambda x: x) + assert obj.some_meth() == 1 + assert p.some_meth() == 1 + obj = None + with pytest.raises(ReferenceError): + p.some_meth() + +[file driver.py] +from native import test_weakref_proxy_with_callback + +test_weakref_proxy_with_callback() From d043cda4bb5777cfa68ab5f2f566736268d0b73b Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Mon, 4 Aug 2025 01:59:22 +0000 Subject: [PATCH 02/24] _weakref stubs --- test-data/unit/lib-stub/_weakref.pyi | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 test-data/unit/lib-stub/_weakref.pyi diff --git a/test-data/unit/lib-stub/_weakref.pyi b/test-data/unit/lib-stub/_weakref.pyi new file mode 100644 index 0000000000000..70012375e6451 --- /dev/null +++ b/test-data/unit/lib-stub/_weakref.pyi @@ -0,0 +1,11 @@ +from typing import Any, Callable, TypeVar, overload +from weakref import CallableProxyType + +_C = TypeVar("_C", bound=Callable[..., Any]) +_T = TypeVar("_T") + +# Return CallableProxyType if object is callable, ProxyType otherwise +@overload +def proxy(object: _C, callback: Callable[[_C], Any] | None = None, /) -> CallableProxyType[_C]: ... +@overload +def proxy(object: _T, callback: Callable[[_T], Any] | None = None, /) -> Any: ... From 81760b2eaa3e1783cb6025cc54df4e1405d79089 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Mon, 4 Aug 2025 01:59:22 +0000 Subject: [PATCH 03/24] weakref stubs --- test-data/unit/lib-stub/weakref.pyi | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test-data/unit/lib-stub/weakref.pyi b/test-data/unit/lib-stub/weakref.pyi index 34e01f4d48f1f..f26a52f351a3a 100644 --- a/test-data/unit/lib-stub/weakref.pyi +++ b/test-data/unit/lib-stub/weakref.pyi @@ -1,7 +1,9 @@ +from _weakref import proxy from collections.abc import Callable -from typing import Any, Generic, TypeVar +from typing import Any, ClassVar, Generic, TypeVar, final from typing_extensions import Self +_C = TypeVar("_C", bound=Callable[..., Any]) _T = TypeVar("_T") class ReferenceType(Generic[_T]): # "weakref" @@ -9,3 +11,12 @@ class ReferenceType(Generic[_T]): # "weakref" def __new__(cls, o: _T, callback: Callable[[Self], Any] | None = ..., /) -> Self: ... ref = ReferenceType + +@final +class CallableProxyType(Generic[_C]): # "weakcallableproxy" + def __eq__(self, value: object, /) -> bool: ... + def __getattr__(self, attr: str) -> Any: ... + __call__: _C + __hash__: ClassVar[None] # type: ignore[assignment] + +__all__ = ["proxy"] From e7906258287b1cdddcb0e626b54cd82bbdbc8dcf Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Mon, 4 Aug 2025 01:59:22 +0000 Subject: [PATCH 04/24] feat: new primitive for weakref.ref --- mypyc/test-data/run-weakref.test | 1 + 1 file changed, 1 insertion(+) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index 892c29c50521d..1a95d6fbe4ffa 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -1,6 +1,7 @@ # Test cases for weakrefs (compile and run) [case testWeakrefRef] +import pytest # type: ignore [import-not-found] from weakref import ref from mypy_extensions import mypyc_attr From 1bcc358f31a51412dc5474024f94307f741b210f Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Mon, 4 Aug 2025 01:59:22 +0000 Subject: [PATCH 05/24] feat(test): test ir --- mypyc/test-data/irbuild-weakref.test | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/mypyc/test-data/irbuild-weakref.test b/mypyc/test-data/irbuild-weakref.test index 58ac6417d2970..2180b1e747aab 100644 --- a/mypyc/test-data/irbuild-weakref.test +++ b/mypyc/test-data/irbuild-weakref.test @@ -49,3 +49,55 @@ def f(x, cb): L0: r0 = PyWeakref_NewRef(x, cb) return r0 + +[case testWeakrefProxy] +import weakref +from typing import Any, Callable +def f(x: object) -> object: + return weakref.proxy(x) + +[out] +def f(x): + x, r0 :: object +L0: + r0 = PyWeakref_NewProxy(x, 0) + return r0 + +[case testWeakrefProxyCallback] +import weakref +from typing import Any, Callable +def f(x: object, cb: Callable[[object], Any]) -> object: + return weakref.proxy(x, cb) + +[out] +def f(x, cb): + x, cb, r0 :: object +L0: + r0 = PyWeakref_NewProxy(x, cb) + return r0 + +[case testFromWeakrefProxy] +from typing import Any, Callable +from weakref import proxy +def f(x: object) -> object: + return proxy(x) + +[out] +def f(x): + x, r0 :: object +L0: + r0 = PyWeakref_NewProxy(x, 0) + return r0 + +[case testFromWeakrefProxyCallback] +from typing import Any, Callable +from weakref import proxy +def f(x: object, cb: Callable[[object], Any]) -> object: + return proxy(x, cb) + +[out] +def f(x, cb): + x, cb, r0 :: object +L0: + r0 = PyWeakref_NewProxy(x, cb) + return r0 From 996987c81bac8b4a3bc230986174b94c069fb1df Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Mon, 4 Aug 2025 01:59:22 +0000 Subject: [PATCH 06/24] fix: steal maybe --- mypyc/primitives/weakref_ops.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mypyc/primitives/weakref_ops.py b/mypyc/primitives/weakref_ops.py index a7ac035b22a4e..48bcdb829cee9 100644 --- a/mypyc/primitives/weakref_ops.py +++ b/mypyc/primitives/weakref_ops.py @@ -20,3 +20,22 @@ c_function_name="PyWeakref_NewRef", error_kind=ERR_MAGIC, ) + +new_proxy_op = function_op( + name="_weakref.proxy", + arg_types=[object_rprimitive], + steals=[True], + return_type=object_rprimitive, + c_function_name="PyWeakref_NewProxy", + extra_int_constants=[(0, pointer_rprimitive)], + error_kind=ERR_MAGIC, +) + +new_proxy_with_callback_op = function_op( + name="_weakref.proxy", + arg_types=[object_rprimitive, object_rprimitive], + steals=[True, False], + return_type=object_rprimitive, + c_function_name="PyWeakref_NewProxy", + error_kind=ERR_MAGIC, +) From 3bcada69bbbb951d2b298d39e33cdab97cac3798 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Mon, 4 Aug 2025 01:59:22 +0000 Subject: [PATCH 07/24] feat(test): split up run tests --- mypyc/test-data/run-weakref.test | 1 - 1 file changed, 1 deletion(-) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index 1a95d6fbe4ffa..892c29c50521d 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -1,7 +1,6 @@ # Test cases for weakrefs (compile and run) [case testWeakrefRef] -import pytest # type: ignore [import-not-found] from weakref import ref from mypy_extensions import mypyc_attr From 9d6c52ccae94eba732d8eb063be351f5a98502de Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Mon, 4 Aug 2025 01:59:22 +0000 Subject: [PATCH 08/24] Update weakref_ops.py --- mypyc/primitives/weakref_ops.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mypyc/primitives/weakref_ops.py b/mypyc/primitives/weakref_ops.py index 48bcdb829cee9..85e07cdb85b7a 100644 --- a/mypyc/primitives/weakref_ops.py +++ b/mypyc/primitives/weakref_ops.py @@ -24,7 +24,6 @@ new_proxy_op = function_op( name="_weakref.proxy", arg_types=[object_rprimitive], - steals=[True], return_type=object_rprimitive, c_function_name="PyWeakref_NewProxy", extra_int_constants=[(0, pointer_rprimitive)], From 1c96658793574f49d2e33a954f1790d0200d2b84 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Mon, 4 Aug 2025 01:59:22 +0000 Subject: [PATCH 09/24] finalize PR --- mypyc/primitives/weakref_ops.py | 2 +- mypyc/test-data/fixtures/test-weakref.pyi | 10 ++++++++++ mypyc/test-data/run-weakref.test | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 mypyc/test-data/fixtures/test-weakref.pyi diff --git a/mypyc/primitives/weakref_ops.py b/mypyc/primitives/weakref_ops.py index 85e07cdb85b7a..f7a7406db8f94 100644 --- a/mypyc/primitives/weakref_ops.py +++ b/mypyc/primitives/weakref_ops.py @@ -33,7 +33,7 @@ new_proxy_with_callback_op = function_op( name="_weakref.proxy", arg_types=[object_rprimitive, object_rprimitive], - steals=[True, False], + #steals=[True, False], return_type=object_rprimitive, c_function_name="PyWeakref_NewProxy", error_kind=ERR_MAGIC, diff --git a/mypyc/test-data/fixtures/test-weakref.pyi b/mypyc/test-data/fixtures/test-weakref.pyi new file mode 100644 index 0000000000000..80525fe07d6ad --- /dev/null +++ b/mypyc/test-data/fixtures/test-weakref.pyi @@ -0,0 +1,10 @@ +class dict: ... +class ellipsis: ... +class int: ... +class str: ... +class tuple: ... +class type: ... +class list: ... +class BaseException: ... +class Exception(BaseException): ... +class ReferenceError(Exception): ... \ No newline at end of file diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index 892c29c50521d..0b070b1567fbb 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -58,10 +58,12 @@ def test_weakref_proxy(): p = proxy(obj) assert obj.some_meth() == 1 assert p.some_meth() == 1 + obj.some_meth() obj = None with pytest.raises(ReferenceError): p.some_meth() +[builtins fixtures/test-weakref.pyi] [file driver.py] from native import test_weakref_proxy @@ -84,10 +86,12 @@ def test_weakref_proxy_with_callback(): p = proxy(obj, lambda x: x) assert obj.some_meth() == 1 assert p.some_meth() == 1 + obj.some_meth() obj = None with pytest.raises(ReferenceError): p.some_meth() +[builtins fixtures/test-weakref.pyi] [file driver.py] from native import test_weakref_proxy_with_callback From 652de32a9f2d68cf2fb974c787ea46f5e9af072d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 Aug 2025 01:59:22 +0000 Subject: [PATCH 10/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypyc/primitives/weakref_ops.py | 2 +- mypyc/test-data/fixtures/test-weakref.pyi | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mypyc/primitives/weakref_ops.py b/mypyc/primitives/weakref_ops.py index f7a7406db8f94..21379d3b2c820 100644 --- a/mypyc/primitives/weakref_ops.py +++ b/mypyc/primitives/weakref_ops.py @@ -33,7 +33,7 @@ new_proxy_with_callback_op = function_op( name="_weakref.proxy", arg_types=[object_rprimitive, object_rprimitive], - #steals=[True, False], + # steals=[True, False], return_type=object_rprimitive, c_function_name="PyWeakref_NewProxy", error_kind=ERR_MAGIC, diff --git a/mypyc/test-data/fixtures/test-weakref.pyi b/mypyc/test-data/fixtures/test-weakref.pyi index 80525fe07d6ad..73896835114bb 100644 --- a/mypyc/test-data/fixtures/test-weakref.pyi +++ b/mypyc/test-data/fixtures/test-weakref.pyi @@ -7,4 +7,4 @@ class type: ... class list: ... class BaseException: ... class Exception(BaseException): ... -class ReferenceError(Exception): ... \ No newline at end of file +class ReferenceError(Exception): ... From 23d89a261a33125f786b1fe7954b46c72701c054 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sun, 17 Aug 2025 12:18:34 -0400 Subject: [PATCH 11/24] rip out pytest.raises --- mypyc/test-data/run-weakref.test | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index 0b070b1567fbb..287006ab0c378 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -43,7 +43,6 @@ test_weakref_ref_with_callback() [case testWeakrefProxy] -import pytest # type: ignore [import-not-found] from weakref import proxy from mypy_extensions import mypyc_attr @@ -60,7 +59,7 @@ def test_weakref_proxy(): assert p.some_meth() == 1 obj.some_meth() obj = None - with pytest.raises(ReferenceError): + with assertRaises(ReferenceError): p.some_meth() [builtins fixtures/test-weakref.pyi] @@ -71,7 +70,6 @@ test_weakref_proxy() [case testWeakrefProxyWithCallback] -import pytest # type: ignore [import-not-found] from weakref import proxy from mypy_extensions import mypyc_attr @@ -88,7 +86,7 @@ def test_weakref_proxy_with_callback(): assert p.some_meth() == 1 obj.some_meth() obj = None - with pytest.raises(ReferenceError): + with assertRaises(ReferenceError): p.some_meth() [builtins fixtures/test-weakref.pyi] From c4f596e68710fdcbdb2ae1ce9b98bc3f6bbae682 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sun, 17 Aug 2025 12:19:46 -0400 Subject: [PATCH 12/24] rip out driver.py --- mypyc/test-data/run-weakref.test | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index 287006ab0c378..8c01ea330c5bc 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -15,11 +15,6 @@ def test_weakref_ref(): obj = None assert r() is None, r() -[file driver.py] -from native import test_weakref_ref - -test_weakref_ref() - [case testWeakrefRefWithCallback] from weakref import ref @@ -36,11 +31,6 @@ def test_weakref_ref_with_callback(): obj = None assert r() is None, r() -[file driver.py] -from native import test_weakref_ref_with_callback - -test_weakref_ref_with_callback() - [case testWeakrefProxy] from weakref import proxy @@ -63,10 +53,6 @@ def test_weakref_proxy(): p.some_meth() [builtins fixtures/test-weakref.pyi] -[file driver.py] -from native import test_weakref_proxy - -test_weakref_proxy() [case testWeakrefProxyWithCallback] @@ -90,7 +76,3 @@ def test_weakref_proxy_with_callback(): p.some_meth() [builtins fixtures/test-weakref.pyi] -[file driver.py] -from native import test_weakref_proxy_with_callback - -test_weakref_proxy_with_callback() From fe03dd6300073073fd56c89e75da0b543b4b2ebb Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sun, 17 Aug 2025 12:21:30 -0400 Subject: [PATCH 13/24] combine run tests --- mypyc/test-data/run-weakref.test | 37 +++----------------------------- 1 file changed, 3 insertions(+), 34 deletions(-) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index 8c01ea330c5bc..9e203d885e61a 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -1,12 +1,14 @@ # Test cases for weakrefs (compile and run) [case testWeakrefRef] -from weakref import ref +from weakref import proxy, ref from mypy_extensions import mypyc_attr @mypyc_attr(native_class=False) class Object: """some random weakreffable object""" + def some_meth(self) -> int: + return 1 def test_weakref_ref(): obj = Object() @@ -15,15 +17,6 @@ def test_weakref_ref(): obj = None assert r() is None, r() - -[case testWeakrefRefWithCallback] -from weakref import ref -from mypy_extensions import mypyc_attr - -@mypyc_attr(native_class=False) -class Object: - """some random weakreffable object""" - def test_weakref_ref_with_callback(): obj = Object() r = ref(obj, lambda x: x) @@ -31,17 +24,6 @@ def test_weakref_ref_with_callback(): obj = None assert r() is None, r() - -[case testWeakrefProxy] -from weakref import proxy -from mypy_extensions import mypyc_attr - -@mypyc_attr(native_class=False) -class Object: - """some random weakreffable object""" - def some_meth(self) -> int: - return 1 - def test_weakref_proxy(): obj = Object() p = proxy(obj) @@ -52,19 +34,6 @@ def test_weakref_proxy(): with assertRaises(ReferenceError): p.some_meth() -[builtins fixtures/test-weakref.pyi] - - -[case testWeakrefProxyWithCallback] -from weakref import proxy -from mypy_extensions import mypyc_attr - -@mypyc_attr(native_class=False) -class Object: - """some random weakreffable object""" - def some_meth(self) -> int: - return 1 - def test_weakref_proxy_with_callback(): obj = Object() p = proxy(obj, lambda x: x) From 2667ae88dfa10f372e9d415e975938a613a14b13 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sun, 17 Aug 2025 12:24:13 -0400 Subject: [PATCH 14/24] test callback is called --- mypyc/test-data/run-weakref.test | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index 9e203d885e61a..28c560b78c6e2 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -10,6 +10,8 @@ class Object: def some_meth(self) -> int: return 1 +_callback_called_cache: dict[str, bool] = {"ref": False, "proxy": False} + def test_weakref_ref(): obj = Object() r = ref(obj) @@ -19,10 +21,11 @@ def test_weakref_ref(): def test_weakref_ref_with_callback(): obj = Object() - r = ref(obj, lambda x: x) + r = ref(obj, lambda x: _callback_called_cache["ref"] = True) assert r() is obj obj = None assert r() is None, r() + assert _callback_called_cache["ref"] is True def test_weakref_proxy(): obj = Object() @@ -36,12 +39,13 @@ def test_weakref_proxy(): def test_weakref_proxy_with_callback(): obj = Object() - p = proxy(obj, lambda x: x) + p = proxy(obj, lambda x: _callback_called_cache["proxy"] = True) assert obj.some_meth() == 1 assert p.some_meth() == 1 obj.some_meth() obj = None with assertRaises(ReferenceError): p.some_meth() + assert _callback_called_cache["proxy"] is True [builtins fixtures/test-weakref.pyi] From 4d3982985e5a01b6f060079f1c62816d3a3cd5f4 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sun, 17 Aug 2025 12:42:47 -0400 Subject: [PATCH 15/24] Update run-weakref.test --- mypyc/test-data/run-weakref.test | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index 28c560b78c6e2..18e0c632c7cb3 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -21,7 +21,7 @@ def test_weakref_ref(): def test_weakref_ref_with_callback(): obj = Object() - r = ref(obj, lambda x: _callback_called_cache["ref"] = True) + r = ref(obj, lambda x: _callback_called_cache.__setitem__("ref", True)) assert r() is obj obj = None assert r() is None, r() @@ -39,7 +39,7 @@ def test_weakref_proxy(): def test_weakref_proxy_with_callback(): obj = Object() - p = proxy(obj, lambda x: _callback_called_cache["proxy"] = True) + p = proxy(obj, lambda x: _callback_called_cache.__setitem__("proxy", True)) assert obj.some_meth() == 1 assert p.some_meth() == 1 obj.some_meth() From 78694f32856a022bb3a49351653cafdc2b9b5809 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sun, 17 Aug 2025 23:47:12 -0400 Subject: [PATCH 16/24] Update run-weakref.test --- mypyc/test-data/run-weakref.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index 18e0c632c7cb3..c42a4abdd05d1 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -10,7 +10,7 @@ class Object: def some_meth(self) -> int: return 1 -_callback_called_cache: dict[str, bool] = {"ref": False, "proxy": False} +_callback_called_cache = {"ref": False, "proxy": False} def test_weakref_ref(): obj = Object() From 5c9cab57170800ba472f947665cbecfb4c52ee1a Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sun, 17 Aug 2025 23:56:29 -0400 Subject: [PATCH 17/24] Update run-weakref.test --- mypyc/test-data/run-weakref.test | 1 + 1 file changed, 1 insertion(+) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index c42a4abdd05d1..c9c845d643d65 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -3,6 +3,7 @@ [case testWeakrefRef] from weakref import proxy, ref from mypy_extensions import mypyc_attr +from testutil import assertRaises @mypyc_attr(native_class=False) class Object: From da8e23a42f4fb7babaf87797f6a314459293afc1 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Fri, 29 Aug 2025 19:52:15 +0000 Subject: [PATCH 18/24] remove test-weakref.pyi --- mypyc/test-data/fixtures/ir.py | 1 + mypyc/test-data/fixtures/test-weakref.pyi | 10 ---------- mypyc/test-data/run-weakref.test | 2 -- 3 files changed, 1 insertion(+), 12 deletions(-) delete mode 100644 mypyc/test-data/fixtures/test-weakref.pyi diff --git a/mypyc/test-data/fixtures/ir.py b/mypyc/test-data/fixtures/ir.py index 661ae50fd5f3c..dba33fd504635 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -340,6 +340,7 @@ class RuntimeError(Exception): pass class UnicodeEncodeError(RuntimeError): pass class UnicodeDecodeError(RuntimeError): pass class NotImplementedError(RuntimeError): pass +class ReferenceError(Exception): pass class StopIteration(Exception): value: Any diff --git a/mypyc/test-data/fixtures/test-weakref.pyi b/mypyc/test-data/fixtures/test-weakref.pyi deleted file mode 100644 index 73896835114bb..0000000000000 --- a/mypyc/test-data/fixtures/test-weakref.pyi +++ /dev/null @@ -1,10 +0,0 @@ -class dict: ... -class ellipsis: ... -class int: ... -class str: ... -class tuple: ... -class type: ... -class list: ... -class BaseException: ... -class Exception(BaseException): ... -class ReferenceError(Exception): ... diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index c9c845d643d65..f69a6812f2acd 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -48,5 +48,3 @@ def test_weakref_proxy_with_callback(): with assertRaises(ReferenceError): p.some_meth() assert _callback_called_cache["proxy"] is True - -[builtins fixtures/test-weakref.pyi] From 05513a8bb40734b790250677e2dde507b298f69a Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Tue, 2 Sep 2025 10:10:57 -0400 Subject: [PATCH 19/24] add type annotations --- mypyc/test-data/run-weakref.test | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index f69a6812f2acd..ababa1ccfc5e7 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -13,14 +13,14 @@ class Object: _callback_called_cache = {"ref": False, "proxy": False} -def test_weakref_ref(): +def test_weakref_ref() -> None: obj = Object() r = ref(obj) assert r() is obj obj = None assert r() is None, r() -def test_weakref_ref_with_callback(): +def test_weakref_ref_with_callback() -> None: obj = Object() r = ref(obj, lambda x: _callback_called_cache.__setitem__("ref", True)) assert r() is obj @@ -28,7 +28,7 @@ def test_weakref_ref_with_callback(): assert r() is None, r() assert _callback_called_cache["ref"] is True -def test_weakref_proxy(): +def test_weakref_proxy() -> None: obj = Object() p = proxy(obj) assert obj.some_meth() == 1 @@ -38,7 +38,7 @@ def test_weakref_proxy(): with assertRaises(ReferenceError): p.some_meth() -def test_weakref_proxy_with_callback(): +def test_weakref_proxy_with_callback() -> None: obj = Object() p = proxy(obj, lambda x: _callback_called_cache.__setitem__("proxy", True)) assert obj.some_meth() == 1 From fd7632c6ea81bcbf9e3c61b268ddfe0b8bd173c9 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 4 Sep 2025 11:18:14 -0400 Subject: [PATCH 20/24] add `ReferenceType.__call__` to stubs --- test-data/unit/lib-stub/weakref.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/test-data/unit/lib-stub/weakref.pyi b/test-data/unit/lib-stub/weakref.pyi index f26a52f351a3a..7d11b65d45481 100644 --- a/test-data/unit/lib-stub/weakref.pyi +++ b/test-data/unit/lib-stub/weakref.pyi @@ -9,6 +9,7 @@ _T = TypeVar("_T") class ReferenceType(Generic[_T]): # "weakref" __callback__: Callable[[Self], Any] def __new__(cls, o: _T, callback: Callable[[Self], Any] | None = ..., /) -> Self: ... + def __call__(self) -> _T | None: ... ref = ReferenceType From fb9e02d42be9212df35239f8f5e0b51e685d9c0a Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 4 Sep 2025 11:38:15 -0400 Subject: [PATCH 21/24] fix mypy errs --- mypyc/test-data/run-weakref.test | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index ababa1ccfc5e7..2629d549f079b 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -14,14 +14,14 @@ class Object: _callback_called_cache = {"ref": False, "proxy": False} def test_weakref_ref() -> None: - obj = Object() + obj: Object | None = Object() r = ref(obj) assert r() is obj obj = None assert r() is None, r() def test_weakref_ref_with_callback() -> None: - obj = Object() + obj: Object | None = Object() r = ref(obj, lambda x: _callback_called_cache.__setitem__("ref", True)) assert r() is obj obj = None @@ -29,7 +29,7 @@ def test_weakref_ref_with_callback() -> None: assert _callback_called_cache["ref"] is True def test_weakref_proxy() -> None: - obj = Object() + obj: Object | None = Object() p = proxy(obj) assert obj.some_meth() == 1 assert p.some_meth() == 1 @@ -39,7 +39,7 @@ def test_weakref_proxy() -> None: p.some_meth() def test_weakref_proxy_with_callback() -> None: - obj = Object() + obj: Object | None = Object() p = proxy(obj, lambda x: _callback_called_cache.__setitem__("proxy", True)) assert obj.some_meth() == 1 assert p.some_meth() == 1 From 8f5bf72bd1bfc2640299364ba225ccec06016937 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 4 Sep 2025 11:53:12 -0400 Subject: [PATCH 22/24] disable error code union-attr --- mypyc/test-data/run-weakref.test | 1 + 1 file changed, 1 insertion(+) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index 2629d549f079b..cd8feccf6a627 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -1,6 +1,7 @@ # Test cases for weakrefs (compile and run) [case testWeakrefRef] +# mypy: disable-error-code="union-attr" from weakref import proxy, ref from mypy_extensions import mypyc_attr from testutil import assertRaises From 5563d05ff3704979e7cfbac4403b156dc378f43d Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 4 Sep 2025 12:00:37 -0400 Subject: [PATCH 23/24] fix: use Optional instead of | --- mypyc/test-data/run-weakref.test | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mypyc/test-data/run-weakref.test b/mypyc/test-data/run-weakref.test index cd8feccf6a627..0a0e180d635d7 100644 --- a/mypyc/test-data/run-weakref.test +++ b/mypyc/test-data/run-weakref.test @@ -5,6 +5,7 @@ from weakref import proxy, ref from mypy_extensions import mypyc_attr from testutil import assertRaises +from typing import Optional @mypyc_attr(native_class=False) class Object: @@ -15,14 +16,14 @@ class Object: _callback_called_cache = {"ref": False, "proxy": False} def test_weakref_ref() -> None: - obj: Object | None = Object() + obj: Optional[Object] = Object() r = ref(obj) assert r() is obj obj = None assert r() is None, r() def test_weakref_ref_with_callback() -> None: - obj: Object | None = Object() + obj: Optional[Object] = Object() r = ref(obj, lambda x: _callback_called_cache.__setitem__("ref", True)) assert r() is obj obj = None @@ -30,7 +31,7 @@ def test_weakref_ref_with_callback() -> None: assert _callback_called_cache["ref"] is True def test_weakref_proxy() -> None: - obj: Object | None = Object() + obj: Optional[Object] = Object() p = proxy(obj) assert obj.some_meth() == 1 assert p.some_meth() == 1 @@ -40,7 +41,7 @@ def test_weakref_proxy() -> None: p.some_meth() def test_weakref_proxy_with_callback() -> None: - obj: Object | None = Object() + obj: Optional[Object] = Object() p = proxy(obj, lambda x: _callback_called_cache.__setitem__("proxy", True)) assert obj.some_meth() == 1 assert p.some_meth() == 1 From 796d5ce4ce52089b3d636cada76fcb6cd48614cd Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Fri, 5 Sep 2025 03:09:45 +0000 Subject: [PATCH 24/24] fix stubs --- test-data/unit/lib-stub/_weakref.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test-data/unit/lib-stub/_weakref.pyi b/test-data/unit/lib-stub/_weakref.pyi index 70012375e6451..50c59b65e2677 100644 --- a/test-data/unit/lib-stub/_weakref.pyi +++ b/test-data/unit/lib-stub/_weakref.pyi @@ -1,11 +1,11 @@ from typing import Any, Callable, TypeVar, overload -from weakref import CallableProxyType +from weakref import CallableProxyType, ProxyType _C = TypeVar("_C", bound=Callable[..., Any]) _T = TypeVar("_T") # Return CallableProxyType if object is callable, ProxyType otherwise @overload -def proxy(object: _C, callback: Callable[[_C], Any] | None = None, /) -> CallableProxyType[_C]: ... +def proxy(object: _C, callback: Callable[[CallableProxyType[_C]], Any] | None = None, /) -> CallableProxyType[_C]: ... @overload -def proxy(object: _T, callback: Callable[[_T], Any] | None = None, /) -> Any: ... +def proxy(object: _T, callback: Callable[[ProxyType[_T]], Any] | None = None, /) -> ProxyType[_T]: ...