1import sys
2from _typeshed import SupportsItems, SupportsLessThan
3from typing import (
4    Any,
5    Callable,
6    Dict,
7    Generic,
8    Hashable,
9    Iterable,
10    Mapping,
11    NamedTuple,
12    Optional,
13    Sequence,
14    Set,
15    Sized,
16    Tuple,
17    Type,
18    TypeVar,
19    Union,
20    overload,
21)
22
23if sys.version_info >= (3, 9):
24    from types import GenericAlias
25
26_AnyCallable = Callable[..., Any]
27
28_T = TypeVar("_T")
29_S = TypeVar("_S")
30
31@overload
32def reduce(function: Callable[[_T, _S], _T], sequence: Iterable[_S], initial: _T) -> _T: ...
33@overload
34def reduce(function: Callable[[_T, _T], _T], sequence: Iterable[_T]) -> _T: ...
35
36class _CacheInfo(NamedTuple):
37    hits: int
38    misses: int
39    maxsize: int
40    currsize: int
41
42class _lru_cache_wrapper(Generic[_T]):
43    __wrapped__: Callable[..., _T]
44    def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T: ...
45    def cache_info(self) -> _CacheInfo: ...
46    def cache_clear(self) -> None: ...
47
48if sys.version_info >= (3, 8):
49    @overload
50    def lru_cache(maxsize: Optional[int] = ..., typed: bool = ...) -> Callable[[Callable[..., _T]], _lru_cache_wrapper[_T]]: ...
51    @overload
52    def lru_cache(maxsize: Callable[..., _T], typed: bool = ...) -> _lru_cache_wrapper[_T]: ...
53
54else:
55    def lru_cache(maxsize: Optional[int] = ..., typed: bool = ...) -> Callable[[Callable[..., _T]], _lru_cache_wrapper[_T]]: ...
56
57WRAPPER_ASSIGNMENTS: Sequence[str]
58WRAPPER_UPDATES: Sequence[str]
59
60def update_wrapper(wrapper: _T, wrapped: _AnyCallable, assigned: Sequence[str] = ..., updated: Sequence[str] = ...) -> _T: ...
61def wraps(wrapped: _AnyCallable, assigned: Sequence[str] = ..., updated: Sequence[str] = ...) -> Callable[[_T], _T]: ...
62def total_ordering(cls: Type[_T]) -> Type[_T]: ...
63def cmp_to_key(mycmp: Callable[[_T, _T], int]) -> Callable[[_T], SupportsLessThan]: ...
64
65class partial(Generic[_T]):
66    func: Callable[..., _T]
67    args: Tuple[Any, ...]
68    keywords: Dict[str, Any]
69    def __init__(self, func: Callable[..., _T], *args: Any, **kwargs: Any) -> None: ...
70    def __call__(self, *args: Any, **kwargs: Any) -> _T: ...
71    if sys.version_info >= (3, 9):
72        def __class_getitem__(cls, item: Any) -> GenericAlias: ...
73
74# With protocols, this could change into a generic protocol that defines __get__ and returns _T
75_Descriptor = Any
76
77class partialmethod(Generic[_T]):
78    func: Union[Callable[..., _T], _Descriptor]
79    args: Tuple[Any, ...]
80    keywords: Dict[str, Any]
81    @overload
82    def __init__(self, __func: Callable[..., _T], *args: Any, **keywords: Any) -> None: ...
83    @overload
84    def __init__(self, __func: _Descriptor, *args: Any, **keywords: Any) -> None: ...
85    def __get__(self, obj: Any, cls: Type[Any]) -> Callable[..., _T]: ...
86    @property
87    def __isabstractmethod__(self) -> bool: ...
88    if sys.version_info >= (3, 9):
89        def __class_getitem__(cls, item: Any) -> GenericAlias: ...
90
91class _SingleDispatchCallable(Generic[_T]):
92    registry: Mapping[Any, Callable[..., _T]]
93    def dispatch(self, cls: Any) -> Callable[..., _T]: ...
94    # @fun.register(complex)
95    # def _(arg, verbose=False): ...
96    @overload
97    def register(self, cls: Type[Any], func: None = ...) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ...
98    # @fun.register
99    # def _(arg: int, verbose=False):
100    @overload
101    def register(self, cls: Callable[..., _T], func: None = ...) -> Callable[..., _T]: ...
102    # fun.register(int, lambda x: x)
103    @overload
104    def register(self, cls: Type[Any], func: Callable[..., _T]) -> Callable[..., _T]: ...
105    def _clear_cache(self) -> None: ...
106    def __call__(self, *args: Any, **kwargs: Any) -> _T: ...
107
108def singledispatch(func: Callable[..., _T]) -> _SingleDispatchCallable[_T]: ...
109
110if sys.version_info >= (3, 8):
111    class singledispatchmethod(Generic[_T]):
112        dispatcher: _SingleDispatchCallable[_T]
113        func: Callable[..., _T]
114        def __init__(self, func: Callable[..., _T]) -> None: ...
115        @overload
116        def register(self, cls: Type[Any], method: None = ...) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ...
117        @overload
118        def register(self, cls: Callable[..., _T], method: None = ...) -> Callable[..., _T]: ...
119        @overload
120        def register(self, cls: Type[Any], method: Callable[..., _T]) -> Callable[..., _T]: ...
121        def __call__(self, *args: Any, **kwargs: Any) -> _T: ...
122    class cached_property(Generic[_T]):
123        func: Callable[[Any], _T]
124        attrname: Optional[str]
125        def __init__(self, func: Callable[[Any], _T]) -> None: ...
126        @overload
127        def __get__(self, instance: None, owner: Optional[Type[Any]] = ...) -> cached_property[_T]: ...
128        @overload
129        def __get__(self, instance: object, owner: Optional[Type[Any]] = ...) -> _T: ...
130        def __set_name__(self, owner: Type[Any], name: str) -> None: ...
131        if sys.version_info >= (3, 9):
132            def __class_getitem__(cls, item: Any) -> GenericAlias: ...
133
134if sys.version_info >= (3, 9):
135    def cache(__user_function: Callable[..., _T]) -> _lru_cache_wrapper[_T]: ...
136
137def _make_key(
138    args: Tuple[Hashable, ...],
139    kwds: SupportsItems[Any, Any],
140    typed: bool,
141    kwd_mark: Tuple[object, ...] = ...,
142    fasttypes: Set[type] = ...,
143    tuple: type = ...,
144    type: Any = ...,
145    len: Callable[[Sized], int] = ...,
146) -> Hashable: ...
147