Skip to content

Commit

Permalink
resort weakref classes
Browse files Browse the repository at this point in the history
This improves fidelity of naming and inheritance on 3.11+

related to python#3968 and python#11141
  • Loading branch information
tungol committed Dec 14, 2023
1 parent 6c5dc24 commit 87900ff
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
30 changes: 2 additions & 28 deletions stdlib/_weakref.pyi
Original file line number Diff line number Diff line change
@@ -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]: ...

Expand Down
46 changes: 36 additions & 10 deletions stdlib/weakref.pyi
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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: ...
Expand Down

0 comments on commit 87900ff

Please sign in to comment.