__init__.pyi 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from collections.abc import Callable, Hashable, Iterator
  2. from functools import partial
  3. from operator import methodcaller
  4. import sys
  5. from typing import (
  6. Any,
  7. Generic,
  8. Protocol,
  9. TypeVar,
  10. overload,
  11. )
  12. if sys.version_info >= (3, 10):
  13. from typing import Concatenate, ParamSpec
  14. else:
  15. from typing_extensions import Concatenate, ParamSpec
  16. _P = ParamSpec('_P')
  17. _R = TypeVar('_R')
  18. _T = TypeVar('_T')
  19. _R1 = TypeVar('_R1')
  20. _R2 = TypeVar('_R2')
  21. _V = TypeVar('_V')
  22. _S = TypeVar('_S')
  23. _R_co = TypeVar('_R_co', covariant=True)
  24. class _OnceCallable(Protocol[_P, _R]):
  25. saved_result: _R
  26. reset: Callable[[], None]
  27. def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R: ...
  28. class _ProxyMethodCacheWrapper(Protocol[_R_co]):
  29. cache_clear: Callable[[], None]
  30. def __call__(self, *args: Hashable, **kwargs: Hashable) -> _R_co: ...
  31. class _MethodCacheWrapper(Protocol[_R_co]):
  32. def cache_clear(self) -> None: ...
  33. def __call__(self, *args: Hashable, **kwargs: Hashable) -> _R_co: ...
  34. # `compose()` overloads below will cover most use cases.
  35. @overload
  36. def compose(
  37. __func1: Callable[[_R], _T],
  38. __func2: Callable[_P, _R],
  39. /,
  40. ) -> Callable[_P, _T]: ...
  41. @overload
  42. def compose(
  43. __func1: Callable[[_R], _T],
  44. __func2: Callable[[_R1], _R],
  45. __func3: Callable[_P, _R1],
  46. /,
  47. ) -> Callable[_P, _T]: ...
  48. @overload
  49. def compose(
  50. __func1: Callable[[_R], _T],
  51. __func2: Callable[[_R2], _R],
  52. __func3: Callable[[_R1], _R2],
  53. __func4: Callable[_P, _R1],
  54. /,
  55. ) -> Callable[_P, _T]: ...
  56. def once(func: Callable[_P, _R]) -> _OnceCallable[_P, _R]: ...
  57. def method_cache(
  58. method: Callable[..., _R],
  59. cache_wrapper: Callable[[Callable[..., _R]], _MethodCacheWrapper[_R]] = ...,
  60. ) -> _MethodCacheWrapper[_R] | _ProxyMethodCacheWrapper[_R]: ...
  61. def apply(
  62. transform: Callable[[_R], _T]
  63. ) -> Callable[[Callable[_P, _R]], Callable[_P, _T]]: ...
  64. def result_invoke(
  65. action: Callable[[_R], Any]
  66. ) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: ...
  67. def invoke(
  68. f: Callable[_P, _R], /, *args: _P.args, **kwargs: _P.kwargs
  69. ) -> Callable[_P, _R]: ...
  70. class Throttler(Generic[_R]):
  71. last_called: float
  72. func: Callable[..., _R]
  73. max_rate: float
  74. def __init__(
  75. self, func: Callable[..., _R] | Throttler[_R], max_rate: float = ...
  76. ) -> None: ...
  77. def reset(self) -> None: ...
  78. def __call__(self, *args: Any, **kwargs: Any) -> _R: ...
  79. def __get__(self, obj: Any, owner: type[Any] | None = ...) -> Callable[..., _R]: ...
  80. def first_invoke(
  81. func1: Callable[..., Any], func2: Callable[_P, _R]
  82. ) -> Callable[_P, _R]: ...
  83. method_caller: Callable[..., methodcaller]
  84. def retry_call(
  85. func: Callable[..., _R],
  86. cleanup: Callable[..., None] = ...,
  87. retries: int | float = ...,
  88. trap: type[BaseException] | tuple[type[BaseException], ...] = ...,
  89. ) -> _R: ...
  90. def retry(
  91. cleanup: Callable[..., None] = ...,
  92. retries: int | float = ...,
  93. trap: type[BaseException] | tuple[type[BaseException], ...] = ...,
  94. ) -> Callable[[Callable[..., _R]], Callable[..., _R]]: ...
  95. def print_yielded(func: Callable[_P, Iterator[Any]]) -> Callable[_P, None]: ...
  96. def pass_none(
  97. func: Callable[Concatenate[_T, _P], _R]
  98. ) -> Callable[Concatenate[_T, _P], _R]: ...
  99. def assign_params(
  100. func: Callable[..., _R], namespace: dict[str, Any]
  101. ) -> partial[_R]: ...
  102. def save_method_args(
  103. method: Callable[Concatenate[_S, _P], _R]
  104. ) -> Callable[Concatenate[_S, _P], _R]: ...
  105. def except_(
  106. *exceptions: type[BaseException], replace: Any = ..., use: Any = ...
  107. ) -> Callable[[Callable[_P, Any]], Callable[_P, Any]]: ...
  108. def identity(x: _T) -> _T: ...
  109. def bypass_when(
  110. check: _V, *, _op: Callable[[_V], Any] = ...
  111. ) -> Callable[[Callable[[_T], _R]], Callable[[_T], _T | _R]]: ...
  112. def bypass_unless(
  113. check: Any,
  114. ) -> Callable[[Callable[[_T], _R]], Callable[[_T], _T | _R]]: ...