1
1
import sys
2
2
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
12
4
from _weakrefset import WeakSet as WeakSet
13
5
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
15
7
from typing_extensions import ParamSpec , Self
16
8
9
+ if sys .version_info >= (3 , 9 ):
10
+ from types import GenericAlias
11
+
17
12
__all__ = [
18
13
"ref" ,
19
14
"proxy" ,
@@ -40,11 +35,39 @@ _P = ParamSpec("_P")
40
35
41
36
ProxyTypes : tuple [type [Any ], ...]
42
37
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
+
43
69
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 : ...
48
71
def __call__ (self ) -> _CallableT | None : ...
49
72
def __eq__ (self , other : object ) -> bool : ...
50
73
def __ne__ (self , other : object ) -> bool : ...
0 commit comments