Skip to content

resort weakref classes #11165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions stdlib/@tests/stubtest_allowlists/common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 2 additions & 29 deletions stdlib/_weakref.pyi
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
import sys
from collections.abc import Callable
from typing import Any, Generic, TypeVar, final, overload
from typing_extensions import Self

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[[Self], Any]
def __new__(cls, o: _T, callback: Callable[[Self], Any] | None = ..., /) -> Self: ...
def __init__(self, o: _T, callback: Callable[[Self], Any] | None = ..., /) -> None: ...
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]: ...

Expand Down
51 changes: 37 additions & 14 deletions stdlib/weakref.pyi
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
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 import Any, Generic, TypeVar, final, overload
from typing_extensions import ParamSpec, Self

if sys.version_info >= (3, 9):
from types import GenericAlias

__all__ = [
"ref",
"proxy",
Expand All @@ -40,11 +35,39 @@ _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]):
# `ref` is implemented in `C` so positional-only arguments are enforced, but not in `WeakMethod`.
def __new__( # pyright: ignore[reportInconsistentConstructor]
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: ...
Expand Down