From 87900ff34729c25b1a29c4861c2f7c9c8b1ba40a Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Thu, 14 Dec 2023 12:57:52 -0800 Subject: [PATCH 1/5] resort weakref classes This improves fidelity of naming and inheritance on 3.11+ related to https://github.com/python/typeshed/issues/3968 and https://github.com/python/typeshed/issues/11141 --- stdlib/_weakref.pyi | 30 ++--------------------------- stdlib/weakref.pyi | 46 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/stdlib/_weakref.pyi b/stdlib/_weakref.pyi index ce0f681248ab..d0596d21c479 100644 --- a/stdlib/_weakref.pyi +++ b/stdlib/_weakref.pyi @@ -1,36 +1,10 @@ -import sys from collections.abc import Callable -from typing import Any, Generic, TypeVar, overload -from typing_extensions import Self, final - -if sys.version_info >= (3, 9): - from types import GenericAlias +from typing import Any, TypeVar, overload +from weakref import CallableProxyType as CallableProxyType, ProxyType as ProxyType, ReferenceType as ReferenceType, ref as ref _C = TypeVar("_C", bound=Callable[..., Any]) _T = TypeVar("_T") -@final -class CallableProxyType(Generic[_C]): # "weakcallableproxy" - def __eq__(self, __value: object) -> bool: ... - def __getattr__(self, attr: str) -> Any: ... - __call__: _C - -@final -class ProxyType(Generic[_T]): # "weakproxy" - def __eq__(self, __value: object) -> bool: ... - def __getattr__(self, attr: str) -> Any: ... - -class ReferenceType(Generic[_T]): - __callback__: Callable[[ReferenceType[_T]], Any] - def __new__(cls, __o: _T, __callback: Callable[[ReferenceType[_T]], Any] | None = ...) -> Self: ... - def __call__(self) -> _T | None: ... - def __eq__(self, __value: object) -> bool: ... - def __hash__(self) -> int: ... - if sys.version_info >= (3, 9): - def __class_getitem__(cls, item: Any) -> GenericAlias: ... - -ref = ReferenceType - def getweakrefcount(__object: Any) -> int: ... def getweakrefs(__object: Any) -> list[Any]: ... diff --git a/stdlib/weakref.pyi b/stdlib/weakref.pyi index 1bb2eacfb46a..11b870b1baf2 100644 --- a/stdlib/weakref.pyi +++ b/stdlib/weakref.pyi @@ -1,18 +1,13 @@ import sys from _typeshed import SupportsKeysAndGetItem -from _weakref import ( - CallableProxyType as CallableProxyType, - ProxyType as ProxyType, - ReferenceType as ReferenceType, - getweakrefcount as getweakrefcount, - getweakrefs as getweakrefs, - proxy as proxy, - ref as ref, -) +from _weakref import getweakrefcount as getweakrefcount, getweakrefs as getweakrefs, proxy as proxy from _weakrefset import WeakSet as WeakSet from collections.abc import Callable, Iterable, Iterator, Mapping, MutableMapping from typing import Any, Generic, TypeVar, overload -from typing_extensions import ParamSpec, Self +from typing_extensions import ParamSpec, Self, final + +if sys.version_info >= (3, 9): + from types import GenericAlias __all__ = [ "ref", @@ -40,6 +35,37 @@ _P = ParamSpec("_P") ProxyTypes: tuple[type[Any], ...] +# These classes are implemented in C and imported from _weakref at runtime. However, +# they consider themselves to live in the weakref module for sys.version_info >= (3, 11), +# so defining their stubs here means we match their __module__ value. +# Prior to 3.11 they did not declare a module for themselves and ended up looking like they +# came from the builtin module at runtime, which was just wrong, and we won't attempt to +# duplicate that. + +@final +class CallableProxyType(Generic[_CallableT]): # "weakcallableproxy" + def __eq__(self, __value: object) -> bool: ... + def __getattr__(self, attr: str) -> Any: ... + __call__: _CallableT + +@final +class ProxyType(Generic[_T]): # "weakproxy" + def __eq__(self, __value: object) -> bool: ... + def __getattr__(self, attr: str) -> Any: ... + +class ReferenceType(Generic[_T]): # "weakref" + __callback__: Callable[[ReferenceType[_T]], Any] + def __new__(cls, __o: _T, __callback: Callable[[ReferenceType[_T]], Any] | None = ...) -> Self: ... + def __call__(self) -> _T | None: ... + def __eq__(self, __value: object) -> bool: ... + def __hash__(self) -> int: ... + if sys.version_info >= (3, 9): + def __class_getitem__(cls, item: Any) -> GenericAlias: ... + +ref = ReferenceType + +# everything below here is implemented in weakref.py + class WeakMethod(ref[_CallableT]): def __new__(cls, meth: _CallableT, callback: Callable[[Self], object] | None = None) -> Self: ... def __call__(self) -> _CallableT | None: ... From 05474786cfdb938a647a65fae51657b084d6417b Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 1 Oct 2024 18:29:52 -0700 Subject: [PATCH 2/5] udpate --- stdlib/weakref.pyi | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/stdlib/weakref.pyi b/stdlib/weakref.pyi index 356336d9ffdd..b9591edcb5ae 100644 --- a/stdlib/weakref.pyi +++ b/stdlib/weakref.pyi @@ -3,8 +3,8 @@ from _typeshed import SupportsKeysAndGetItem from _weakref import getweakrefcount as getweakrefcount, getweakrefs as getweakrefs, proxy as proxy from _weakrefset import WeakSet as WeakSet from collections.abc import Callable, Iterable, Iterator, Mapping, MutableMapping -from typing import Any, Generic, TypeVar, overload -from typing_extensions import ParamSpec, Self, final +from typing import Any, Generic, TypeVar, final, overload +from typing_extensions import ParamSpec, Self if sys.version_info >= (3, 9): from types import GenericAlias @@ -44,23 +44,23 @@ ProxyTypes: tuple[type[Any], ...] @final class CallableProxyType(Generic[_CallableT]): # "weakcallableproxy" - def __eq__(self, __value: object) -> bool: ... + def __eq__(self, value: object, /) -> bool: ... def __getattr__(self, attr: str) -> Any: ... __call__: _CallableT @final class ProxyType(Generic[_T]): # "weakproxy" - def __eq__(self, __value: object) -> bool: ... + def __eq__(self, value: object, /) -> bool: ... def __getattr__(self, attr: str) -> Any: ... class ReferenceType(Generic[_T]): # "weakref" __callback__: Callable[[ReferenceType[_T]], Any] - def __new__(cls, __o: _T, __callback: Callable[[ReferenceType[_T]], Any] | None = ...) -> Self: ... + def __new__(cls, o: _T, callback: Callable[[ReferenceType[_T]], Any] | None = ..., /) -> Self: ... def __call__(self) -> _T | None: ... - def __eq__(self, __value: object) -> bool: ... + def __eq__(self, value: object, /) -> bool: ... def __hash__(self) -> int: ... if sys.version_info >= (3, 9): - def __class_getitem__(cls, item: Any) -> GenericAlias: ... + def __class_getitem__(cls, item: Any, /) -> GenericAlias: ... ref = ReferenceType From ef5bfb91d26afed7f515e44194a9b819dafb2ff2 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 1 Oct 2024 18:48:56 -0700 Subject: [PATCH 3/5] allowlist --- stdlib/@tests/stubtest_allowlists/common.txt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index 01b23cd7a274..942af553b30f 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -535,10 +535,11 @@ wsgiref.handlers.BaseHandler.headers_sent wsgiref.handlers.BaseHandler.result wsgiref.handlers.BaseHandler.status -(_?weakref.ref|_?weakref.ReferenceType).__call__ # C function default annotation is wrong -_?weakref.CallableProxyType.__getattr__ # Should have all attributes of proxy -_?weakref.ProxyType.__bytes__ # Doesn't actually exist -_?weakref.ProxyType.__getattr__ # Should have all attributes of proxy +_?weakref\.(ref|ReferenceType)\.__init__ # C implementation has incorrect signature +_?weakref\.(ref|ReferenceType)\.__call__ # C function default annotation is wrong +_?weakref\.CallableProxyType\.__getattr__ # Should have all attributes of proxy +_?weakref\.ProxyType\.__bytes__ # Doesn't actually exist +_?weakref\.ProxyType\.__getattr__ # Should have all attributes of proxy weakref.WeakValueDictionary.setdefault # has a default value for the "default" argument, but always errors out if no value is supplied for the parameter by the user xml.dom.minidom.StringTypes # Unnecessary re-export From 65221bd52b6908d50461470f43fe8053e806c348 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 1 Oct 2024 19:00:02 -0700 Subject: [PATCH 4/5] Update weakref.pyi --- stdlib/weakref.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/weakref.pyi b/stdlib/weakref.pyi index b9591edcb5ae..5dbe7781dbfe 100644 --- a/stdlib/weakref.pyi +++ b/stdlib/weakref.pyi @@ -67,8 +67,7 @@ ref = ReferenceType # everything below here is implemented in weakref.py class WeakMethod(ref[_CallableT]): - # `ref` is implemented in `C` so positional-only arguments are enforced, but not in `WeakMethod`. - def __new__( # pyright: ignore[reportInconsistentConstructor] + def __new__( cls, meth: _CallableT, callback: Callable[[Self], Any] | None = None ) -> Self: ... def __call__(self) -> _CallableT | None: ... From 43530ab9af165fd56ba79324397b97a32c271c10 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Oct 2024 02:05:57 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/weakref.pyi | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/stdlib/weakref.pyi b/stdlib/weakref.pyi index 5dbe7781dbfe..1b74c4846b7c 100644 --- a/stdlib/weakref.pyi +++ b/stdlib/weakref.pyi @@ -67,9 +67,7 @@ ref = ReferenceType # everything below here is implemented in weakref.py class WeakMethod(ref[_CallableT]): - def __new__( - cls, meth: _CallableT, callback: Callable[[Self], Any] | None = None - ) -> Self: ... + def __new__(cls, meth: _CallableT, callback: Callable[[Self], Any] | None = None) -> Self: ... def __call__(self) -> _CallableT | None: ... def __eq__(self, other: object) -> bool: ... def __ne__(self, other: object) -> bool: ...