From 0471c999402bfacfb134af433548e015f6b86d77 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Sat, 2 Aug 2025 17:21:46 +0000 Subject: [PATCH 1/3] 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 902c9e407ff4..892c29c50521 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 266993d569e2cd708f98a1a7e8154bb2c8ea8366 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Sat, 2 Aug 2025 17:24:10 +0000 Subject: [PATCH 2/3] _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 000000000000..70012375e645 --- /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 55f1f66ebdba450084f8efc616fe3998a94f0766 Mon Sep 17 00:00:00 2001 From: BobTheBuidler Date: Sat, 2 Aug 2025 17:25:16 +0000 Subject: [PATCH 3/3] 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 34e01f4d48f1..f26a52f351a3 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"]