Skip to content

Commit c438945

Browse files
resort weakref classes (#11165)
This improves fidelity of naming and inheritance on 3.11+ related to #3968 and #11141 Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 6bc1884 commit c438945

File tree

3 files changed

+44
-47
lines changed

3 files changed

+44
-47
lines changed

stdlib/@tests/stubtest_allowlists/common.txt

+5-4
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,11 @@ wsgiref.handlers.BaseHandler.headers_sent
535535
wsgiref.handlers.BaseHandler.result
536536
wsgiref.handlers.BaseHandler.status
537537

538-
(_?weakref.ref|_?weakref.ReferenceType).__call__ # C function default annotation is wrong
539-
_?weakref.CallableProxyType.__getattr__ # Should have all attributes of proxy
540-
_?weakref.ProxyType.__bytes__ # Doesn't actually exist
541-
_?weakref.ProxyType.__getattr__ # Should have all attributes of proxy
538+
_?weakref\.(ref|ReferenceType)\.__init__ # C implementation has incorrect signature
539+
_?weakref\.(ref|ReferenceType)\.__call__ # C function default annotation is wrong
540+
_?weakref\.CallableProxyType\.__getattr__ # Should have all attributes of proxy
541+
_?weakref\.ProxyType\.__bytes__ # Doesn't actually exist
542+
_?weakref\.ProxyType\.__getattr__ # Should have all attributes of proxy
542543
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
543544
xml.dom.minidom.StringTypes # Unnecessary re-export
544545

stdlib/_weakref.pyi

+2-29
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,10 @@
1-
import sys
21
from collections.abc import Callable
3-
from typing import Any, Generic, TypeVar, final, overload
4-
from typing_extensions import Self
5-
6-
if sys.version_info >= (3, 9):
7-
from types import GenericAlias
2+
from typing import Any, TypeVar, overload
3+
from weakref import CallableProxyType as CallableProxyType, ProxyType as ProxyType, ReferenceType as ReferenceType, ref as ref
84

95
_C = TypeVar("_C", bound=Callable[..., Any])
106
_T = TypeVar("_T")
117

12-
@final
13-
class CallableProxyType(Generic[_C]): # "weakcallableproxy"
14-
def __eq__(self, value: object, /) -> bool: ...
15-
def __getattr__(self, attr: str) -> Any: ...
16-
__call__: _C
17-
18-
@final
19-
class ProxyType(Generic[_T]): # "weakproxy"
20-
def __eq__(self, value: object, /) -> bool: ...
21-
def __getattr__(self, attr: str) -> Any: ...
22-
23-
class ReferenceType(Generic[_T]):
24-
__callback__: Callable[[Self], Any]
25-
def __new__(cls, o: _T, callback: Callable[[Self], Any] | None = ..., /) -> Self: ...
26-
def __init__(self, o: _T, callback: Callable[[Self], Any] | None = ..., /) -> None: ...
27-
def __call__(self) -> _T | None: ...
28-
def __eq__(self, value: object, /) -> bool: ...
29-
def __hash__(self) -> int: ...
30-
if sys.version_info >= (3, 9):
31-
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
32-
33-
ref = ReferenceType
34-
358
def getweakrefcount(object: Any, /) -> int: ...
369
def getweakrefs(object: Any, /) -> list[Any]: ...
3710

stdlib/weakref.pyi

+37-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import sys
22
from _typeshed import SupportsKeysAndGetItem
3-
from _weakref import (
4-
CallableProxyType as CallableProxyType,
5-
ProxyType as ProxyType,
6-
ReferenceType as ReferenceType,
7-
getweakrefcount as getweakrefcount,
8-
getweakrefs as getweakrefs,
9-
proxy as proxy,
10-
ref as ref,
11-
)
3+
from _weakref import getweakrefcount as getweakrefcount, getweakrefs as getweakrefs, proxy as proxy
124
from _weakrefset import WeakSet as WeakSet
135
from collections.abc import Callable, Iterable, Iterator, Mapping, MutableMapping
14-
from typing import Any, Generic, TypeVar, overload
6+
from typing import Any, Generic, TypeVar, final, overload
157
from typing_extensions import ParamSpec, Self
168

9+
if sys.version_info >= (3, 9):
10+
from types import GenericAlias
11+
1712
__all__ = [
1813
"ref",
1914
"proxy",
@@ -40,11 +35,39 @@ _P = ParamSpec("_P")
4035

4136
ProxyTypes: tuple[type[Any], ...]
4237

38+
# These classes are implemented in C and imported from _weakref at runtime. However,
39+
# they consider themselves to live in the weakref module for sys.version_info >= (3, 11),
40+
# so defining their stubs here means we match their __module__ value.
41+
# Prior to 3.11 they did not declare a module for themselves and ended up looking like they
42+
# came from the builtin module at runtime, which was just wrong, and we won't attempt to
43+
# duplicate that.
44+
45+
@final
46+
class CallableProxyType(Generic[_CallableT]): # "weakcallableproxy"
47+
def __eq__(self, value: object, /) -> bool: ...
48+
def __getattr__(self, attr: str) -> Any: ...
49+
__call__: _CallableT
50+
51+
@final
52+
class ProxyType(Generic[_T]): # "weakproxy"
53+
def __eq__(self, value: object, /) -> bool: ...
54+
def __getattr__(self, attr: str) -> Any: ...
55+
56+
class ReferenceType(Generic[_T]): # "weakref"
57+
__callback__: Callable[[ReferenceType[_T]], Any]
58+
def __new__(cls, o: _T, callback: Callable[[ReferenceType[_T]], Any] | None = ..., /) -> Self: ...
59+
def __call__(self) -> _T | None: ...
60+
def __eq__(self, value: object, /) -> bool: ...
61+
def __hash__(self) -> int: ...
62+
if sys.version_info >= (3, 9):
63+
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
64+
65+
ref = ReferenceType
66+
67+
# everything below here is implemented in weakref.py
68+
4369
class WeakMethod(ref[_CallableT]):
44-
# `ref` is implemented in `C` so positional-only arguments are enforced, but not in `WeakMethod`.
45-
def __new__( # pyright: ignore[reportInconsistentConstructor]
46-
cls, meth: _CallableT, callback: Callable[[Self], Any] | None = None
47-
) -> Self: ...
70+
def __new__(cls, meth: _CallableT, callback: Callable[[Self], Any] | None = None) -> Self: ...
4871
def __call__(self) -> _CallableT | None: ...
4972
def __eq__(self, other: object) -> bool: ...
5073
def __ne__(self, other: object) -> bool: ...

0 commit comments

Comments
 (0)