Debian Codegraph @

jedi/third_party/typeshed/stdlib/functools.pyi

raw symbols SCIP
1 import sys
2 import types
3 from _typeshed import SupportsAllComparisons, SupportsItems
4 from collections.abc import Callable, Hashable, Iterable, Sized
5 from types import GenericAlias
6 from typing import Any, Final, Generic, Literal, NamedTuple, TypedDict, TypeVar, final, overload, type_check_only
7 from typing_extensions import ParamSpec, Self, TypeAlias, disjoint_base
8
9 __all__ = [
10 "update_wrapper",
11 "wraps",
12 "WRAPPER_ASSIGNMENTS",
13 "WRAPPER_UPDATES",
14 "total_ordering",
15 "cmp_to_key",
16 "lru_cache",
17 "reduce",
18 "partial",
19 "partialmethod",
20 "singledispatch",
21 "cached_property",
22 "singledispatchmethod",
23 "cache",
24 ]
25
26 _T = TypeVar("_T")
27 _T_co = TypeVar("_T_co", covariant=True)
28 _S = TypeVar("_S")
29 _PWrapped = ParamSpec("_PWrapped")
30 _RWrapped = TypeVar("_RWrapped")
31 _PWrapper = ParamSpec("_PWrapper")
32 _RWrapper = TypeVar("_RWrapper")
33
34 if sys.version_info >= (3, 14):
35 @overload
36 def reduce(function: Callable[[_T, _S], _T], iterable: Iterable[_S], /, initial: _T) -> _T: ...
37
38 else:
39 @overload
40 def reduce(function: Callable[[_T, _S], _T], iterable: Iterable[_S], initial: _T, /) -> _T: ...
41
42 @overload
43 def reduce(function: Callable[[_T, _T], _T], iterable: Iterable[_T], /) -> _T: ...
44
45 class _CacheInfo(NamedTuple):
46 hits: int
47 misses: int
48 maxsize: int | None
49 currsize: int
50
51 @type_check_only
52 class _CacheParameters(TypedDict):
53 maxsize: int
54 typed: bool
55
56 @final
57 class _lru_cache_wrapper(Generic[_T_co]):
58 __wrapped__: Callable[..., _T_co]
59 def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T_co: ...
60 def cache_info(self) -> _CacheInfo: ...
61 def cache_clear(self) -> None: ...
62 def cache_parameters(self) -> _CacheParameters: ...
63 def __copy__(self) -> _lru_cache_wrapper[_T_co]: ...
64 def __deepcopy__(self, memo: Any, /) -> _lru_cache_wrapper[_T_co]: ...
65
66 # as with ``Callable``, we'll assume that these attributes exist
67 __name__: str
68 __qualname__: str
69
70 @overload
71 def lru_cache(maxsize: int | None = 128, typed: bool = False) -> Callable[[Callable[..., _T]], _lru_cache_wrapper[_T]]: ...
72 @overload
73 def lru_cache(maxsize: Callable[..., _T], typed: bool = False) -> _lru_cache_wrapper[_T]: ...
74
75 if sys.version_info >= (3, 14):
76 WRAPPER_ASSIGNMENTS: Final[
77 tuple[
78 Literal["__module__"],
79 Literal["__name__"],
80 Literal["__qualname__"],
81 Literal["__doc__"],
82 Literal["__annotate__"],
83 Literal["__type_params__"],
84 ]
85 ]
86 elif sys.version_info >= (3, 12):
87 WRAPPER_ASSIGNMENTS: Final[
88 tuple[
89 Literal["__module__"],
90 Literal["__name__"],
91 Literal["__qualname__"],
92 Literal["__doc__"],
93 Literal["__annotations__"],
94 Literal["__type_params__"],
95 ]
96 ]
97 else:
98 WRAPPER_ASSIGNMENTS: Final[
99 tuple[Literal["__module__"], Literal["__name__"], Literal["__qualname__"], Literal["__doc__"], Literal["__annotations__"]]
100 ]
101
102 WRAPPER_UPDATES: Final[tuple[Literal["__dict__"]]]
103
104 @type_check_only
105 class _Wrapped(Generic[_PWrapped, _RWrapped, _PWrapper, _RWrapper]):
106 __wrapped__: Callable[_PWrapped, _RWrapped]
107 def __call__(self, *args: _PWrapper.args, **kwargs: _PWrapper.kwargs) -> _RWrapper: ...
108 # as with ``Callable``, we'll assume that these attributes exist
109 __name__: str
110 __qualname__: str
111
112 @type_check_only
113 class _Wrapper(Generic[_PWrapped, _RWrapped]):
114 def __call__(self, f: Callable[_PWrapper, _RWrapper]) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWrapper]: ...
115
116 if sys.version_info >= (3, 14):
117 def update_wrapper(
118 wrapper: Callable[_PWrapper, _RWrapper],
119 wrapped: Callable[_PWrapped, _RWrapped],
120 assigned: Iterable[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotate__", "__type_params__"),
121 updated: Iterable[str] = ("__dict__",),
122 ) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWrapper]: ...
123 def wraps(
124 wrapped: Callable[_PWrapped, _RWrapped],
125 assigned: Iterable[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotate__", "__type_params__"),
126 updated: Iterable[str] = ("__dict__",),
127 ) -> _Wrapper[_PWrapped, _RWrapped]: ...
128
129 elif sys.version_info >= (3, 12):
130 def update_wrapper(
131 wrapper: Callable[_PWrapper, _RWrapper],
132 wrapped: Callable[_PWrapped, _RWrapped],
133 assigned: Iterable[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotations__", "__type_params__"),
134 updated: Iterable[str] = ("__dict__",),
135 ) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWrapper]: ...
136 def wraps(
137 wrapped: Callable[_PWrapped, _RWrapped],
138 assigned: Iterable[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotations__", "__type_params__"),
139 updated: Iterable[str] = ("__dict__",),
140 ) -> _Wrapper[_PWrapped, _RWrapped]: ...
141
142 else:
143 def update_wrapper(
144 wrapper: Callable[_PWrapper, _RWrapper],
145 wrapped: Callable[_PWrapped, _RWrapped],
146 assigned: Iterable[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotations__"),
147 updated: Iterable[str] = ("__dict__",),
148 ) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWrapper]: ...
149 def wraps(
150 wrapped: Callable[_PWrapped, _RWrapped],
151 assigned: Iterable[str] = ("__module__", "__name__", "__qualname__", "__doc__", "__annotations__"),
152 updated: Iterable[str] = ("__dict__",),
153 ) -> _Wrapper[_PWrapped, _RWrapped]: ...
154
155 def total_ordering(cls: type[_T]) -> type[_T]: ...
156 def cmp_to_key(mycmp: Callable[[_T, _T], int]) -> Callable[[_T], SupportsAllComparisons]: ...
157 @disjoint_base
158 class partial(Generic[_T]):
159 @property
160 def func(self) -> Callable[..., _T]: ...
161 @property
162 def args(self) -> tuple[Any, ...]: ...
163 @property
164 def keywords(self) -> dict[str, Any]: ...
165 def __new__(cls, func: Callable[..., _T], /, *args: Any, **kwargs: Any) -> Self: ...
166 def __call__(self, /, *args: Any, **kwargs: Any) -> _T: ...
167 def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
168
169 # With protocols, this could change into a generic protocol that defines __get__ and returns _T
170 _Descriptor: TypeAlias = Any
171
172 class partialmethod(Generic[_T]):
173 func: Callable[..., _T] | _Descriptor
174 args: tuple[Any, ...]
175 keywords: dict[str, Any]
176 if sys.version_info >= (3, 14):
177 @overload
178 def __new__(self, func: Callable[..., _T], /, *args: Any, **keywords: Any) -> Self: ...
179 @overload
180 def __new__(self, func: _Descriptor, /, *args: Any, **keywords: Any) -> Self: ...
181 else:
182 @overload
183 def __init__(self, func: Callable[..., _T], /, *args: Any, **keywords: Any) -> None: ...
184 @overload
185 def __init__(self, func: _Descriptor, /, *args: Any, **keywords: Any) -> None: ...
186
187 def __get__(self, obj: Any, cls: type[Any] | None = None) -> Callable[..., _T]: ...
188 @property
189 def __isabstractmethod__(self) -> bool: ...
190 def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
191
192 if sys.version_info >= (3, 11):
193 _RegType: TypeAlias = type[Any] | types.UnionType
194 else:
195 _RegType: TypeAlias = type[Any]
196
197 @type_check_only
198 class _SingleDispatchCallable(Generic[_T]):
199 registry: types.MappingProxyType[Any, Callable[..., _T]]
200 def dispatch(self, cls: Any) -> Callable[..., _T]: ...
201 # @fun.register(complex)
202 # def _(arg, verbose=False): ...
203 @overload
204 def register(self, cls: _RegType, func: None = None) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ...
205 # @fun.register
206 # def _(arg: int, verbose=False):
207 @overload
208 def register(self, cls: Callable[..., _T], func: None = None) -> Callable[..., _T]: ...
209 # fun.register(int, lambda x: x)
210 @overload
211 def register(self, cls: _RegType, func: Callable[..., _T]) -> Callable[..., _T]: ...
212 def _clear_cache(self) -> None: ...
213 def __call__(self, /, *args: Any, **kwargs: Any) -> _T: ...
214
215 def singledispatch(func: Callable[..., _T]) -> _SingleDispatchCallable[_T]: ...
216
217 class singledispatchmethod(Generic[_T]):
218 dispatcher: _SingleDispatchCallable[_T]
219 func: Callable[..., _T]
220 def __init__(self, func: Callable[..., _T]) -> None: ...
221 @property
222 def __isabstractmethod__(self) -> bool: ...
223 @overload
224 def register(self, cls: _RegType, method: None = None) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ...
225 @overload
226 def register(self, cls: Callable[..., _T], method: None = None) -> Callable[..., _T]: ...
227 @overload
228 def register(self, cls: _RegType, method: Callable[..., _T]) -> Callable[..., _T]: ...
229 def __get__(self, obj: _S, cls: type[_S] | None = None) -> Callable[..., _T]: ...
230
231 class cached_property(Generic[_T_co]):
232 func: Callable[[Any], _T_co]
233 attrname: str | None
234 def __init__(self, func: Callable[[Any], _T_co]) -> None: ...
235 @overload
236 def __get__(self, instance: None, owner: type[Any] | None = None) -> Self: ...
237 @overload
238 def __get__(self, instance: object, owner: type[Any] | None = None) -> _T_co: ...
239 def __set_name__(self, owner: type[Any], name: str) -> None: ...
240 # __set__ is not defined at runtime, but @cached_property is designed to be settable
241 def __set__(self, instance: object, value: _T_co) -> None: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues]
242 def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
243
244 def cache(user_function: Callable[..., _T], /) -> _lru_cache_wrapper[_T]: ...
245 def _make_key(
246 args: tuple[Hashable, ...],
247 kwds: SupportsItems[Any, Any],
248 typed: bool,
249 kwd_mark: tuple[object, ...] = ...,
250 fasttypes: set[type] = ...,
251 tuple: type = ...,
252 type: Any = ...,
253 len: Callable[[Sized], int] = ...,
254 ) -> Hashable: ...
255
256 if sys.version_info >= (3, 14):
257 @final
258 class _PlaceholderType: ...
259
260 Placeholder: Final[_PlaceholderType]
261
262 __all__ += ["Placeholder"]