| [case testWeakrefRef] | |
| import weakref | |
| from typing import Any, Callable | |
| def f(x: object) -> object: | |
| return weakref.ref(x) | |
| [out] | |
| def f(x): | |
| x, r0 :: object | |
| L0: | |
| r0 = PyWeakref_NewRef(x, 0) | |
| return r0 | |
| [case testWeakrefRefCallback] | |
| import weakref | |
| from typing import Any, Callable | |
| def f(x: object, cb: Callable[[object], Any]) -> object: | |
| return weakref.ref(x, cb) | |
| [out] | |
| def f(x, cb): | |
| x, cb, r0 :: object | |
| L0: | |
| r0 = PyWeakref_NewRef(x, cb) | |
| return r0 | |
| [case testFromWeakrefRef] | |
| from typing import Any, Callable | |
| from weakref import ref | |
| def f(x: object) -> object: | |
| return ref(x) | |
| [out] | |
| def f(x): | |
| x, r0 :: object | |
| L0: | |
| r0 = PyWeakref_NewRef(x, 0) | |
| return r0 | |
| [case testFromWeakrefRefCallback] | |
| from typing import Any, Callable | |
| from weakref import ref | |
| def f(x: object, cb: Callable[[object], Any]) -> object: | |
| return ref(x, cb) | |
| [out] | |
| def f(x, cb): | |
| x, cb, r0 :: object | |
| 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 | |
| [case testWeakrefToNativeClassErrors] | |
| from typing import Any | |
| import weakref | |
| from mypy_extensions import mypyc_attr, trait | |
| class Plain: | |
| pass | |
| @mypyc_attr(weakrefs=True) | |
| class Supported: | |
| pass | |
| class SupportedSub(Supported): | |
| pass | |
| @mypyc_attr(native_class=False) | |
| class NonNative: | |
| pass | |
| @trait | |
| class T: | |
| pass | |
| def f(a: Plain, b: Supported, c: SupportedSub, d: NonNative, e: T, g: object) -> None: | |
| weakref.ref(a) # E: Cannot create weak reference to instance of native class "__main__.Plain" \ | |
| # N: Add "@mypy_extensions.mypyc_attr(weakrefs=True)" to the class definition to enable support for weak references | |
| weakref.proxy(a) # E: Cannot create weak reference to instance of native class "__main__.Plain" \ | |
| # N: Add "@mypy_extensions.mypyc_attr(weakrefs=True)" to the class definition to enable support for weak references | |
| # These are all fine (or can't be checked statically) | |
| weakref.ref(b) | |
| weakref.ref(c) | |
| weakref.ref(d) | |
| weakref.ref(e) | |
| weakref.ref(g) | |
| [out] | |
| [case testWeakrefAttrErrors] | |
| from mypy_extensions import mypyc_attr, trait | |
| @mypyc_attr(weakrefs=True) # E: "weakrefs" can't be used with traits | |
| @trait | |
| class T: | |
| pass | |
| @mypyc_attr(weakrefs=True) # E: "weakrefs" can't be used in a class that inherits from a built-in type | |
| class Dict(dict): | |
| pass | |
| class Base: | |
| pass | |
| @mypyc_attr(weakrefs=True) # E: "weakrefs" must be used in the base class "Base" as well | |
| class Sub(Base): | |
| pass | |
| [out] |