_callable.pyi 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. """
  2. A module with various ``typing.Protocol`` subclasses that implement
  3. the ``__call__`` magic method.
  4. See the `Mypy documentation`_ on protocols for more details.
  5. .. _`Mypy documentation`: https://mypy.readthedocs.io/en/stable/protocols.html#callback-protocols
  6. """
  7. from __future__ import annotations
  8. from typing import (
  9. TypeVar,
  10. overload,
  11. Any,
  12. NoReturn,
  13. Protocol,
  14. )
  15. from numpy import (
  16. ndarray,
  17. dtype,
  18. generic,
  19. bool_,
  20. timedelta64,
  21. number,
  22. integer,
  23. unsignedinteger,
  24. signedinteger,
  25. int8,
  26. int_,
  27. floating,
  28. float64,
  29. complexfloating,
  30. complex128,
  31. )
  32. from ._nbit import _NBitInt, _NBitDouble
  33. from ._scalars import (
  34. _BoolLike_co,
  35. _IntLike_co,
  36. _FloatLike_co,
  37. _NumberLike_co,
  38. )
  39. from . import NBitBase
  40. from ._generic_alias import NDArray
  41. from ._nested_sequence import _NestedSequence
  42. _T1 = TypeVar("_T1")
  43. _T2 = TypeVar("_T2")
  44. _T1_contra = TypeVar("_T1_contra", contravariant=True)
  45. _T2_contra = TypeVar("_T2_contra", contravariant=True)
  46. _2Tuple = tuple[_T1, _T1]
  47. _NBit1 = TypeVar("_NBit1", bound=NBitBase)
  48. _NBit2 = TypeVar("_NBit2", bound=NBitBase)
  49. _IntType = TypeVar("_IntType", bound=integer)
  50. _FloatType = TypeVar("_FloatType", bound=floating)
  51. _NumberType = TypeVar("_NumberType", bound=number)
  52. _NumberType_co = TypeVar("_NumberType_co", covariant=True, bound=number)
  53. _GenericType_co = TypeVar("_GenericType_co", covariant=True, bound=generic)
  54. class _BoolOp(Protocol[_GenericType_co]):
  55. @overload
  56. def __call__(self, other: _BoolLike_co, /) -> _GenericType_co: ...
  57. @overload # platform dependent
  58. def __call__(self, other: int, /) -> int_: ...
  59. @overload
  60. def __call__(self, other: float, /) -> float64: ...
  61. @overload
  62. def __call__(self, other: complex, /) -> complex128: ...
  63. @overload
  64. def __call__(self, other: _NumberType, /) -> _NumberType: ...
  65. class _BoolBitOp(Protocol[_GenericType_co]):
  66. @overload
  67. def __call__(self, other: _BoolLike_co, /) -> _GenericType_co: ...
  68. @overload # platform dependent
  69. def __call__(self, other: int, /) -> int_: ...
  70. @overload
  71. def __call__(self, other: _IntType, /) -> _IntType: ...
  72. class _BoolSub(Protocol):
  73. # Note that `other: bool_` is absent here
  74. @overload
  75. def __call__(self, other: bool, /) -> NoReturn: ...
  76. @overload # platform dependent
  77. def __call__(self, other: int, /) -> int_: ...
  78. @overload
  79. def __call__(self, other: float, /) -> float64: ...
  80. @overload
  81. def __call__(self, other: complex, /) -> complex128: ...
  82. @overload
  83. def __call__(self, other: _NumberType, /) -> _NumberType: ...
  84. class _BoolTrueDiv(Protocol):
  85. @overload
  86. def __call__(self, other: float | _IntLike_co, /) -> float64: ...
  87. @overload
  88. def __call__(self, other: complex, /) -> complex128: ...
  89. @overload
  90. def __call__(self, other: _NumberType, /) -> _NumberType: ...
  91. class _BoolMod(Protocol):
  92. @overload
  93. def __call__(self, other: _BoolLike_co, /) -> int8: ...
  94. @overload # platform dependent
  95. def __call__(self, other: int, /) -> int_: ...
  96. @overload
  97. def __call__(self, other: float, /) -> float64: ...
  98. @overload
  99. def __call__(self, other: _IntType, /) -> _IntType: ...
  100. @overload
  101. def __call__(self, other: _FloatType, /) -> _FloatType: ...
  102. class _BoolDivMod(Protocol):
  103. @overload
  104. def __call__(self, other: _BoolLike_co, /) -> _2Tuple[int8]: ...
  105. @overload # platform dependent
  106. def __call__(self, other: int, /) -> _2Tuple[int_]: ...
  107. @overload
  108. def __call__(self, other: float, /) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ...
  109. @overload
  110. def __call__(self, other: _IntType, /) -> _2Tuple[_IntType]: ...
  111. @overload
  112. def __call__(self, other: _FloatType, /) -> _2Tuple[_FloatType]: ...
  113. class _TD64Div(Protocol[_NumberType_co]):
  114. @overload
  115. def __call__(self, other: timedelta64, /) -> _NumberType_co: ...
  116. @overload
  117. def __call__(self, other: _BoolLike_co, /) -> NoReturn: ...
  118. @overload
  119. def __call__(self, other: _FloatLike_co, /) -> timedelta64: ...
  120. class _IntTrueDiv(Protocol[_NBit1]):
  121. @overload
  122. def __call__(self, other: bool, /) -> floating[_NBit1]: ...
  123. @overload
  124. def __call__(self, other: int, /) -> floating[_NBit1 | _NBitInt]: ...
  125. @overload
  126. def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ...
  127. @overload
  128. def __call__(
  129. self, other: complex, /,
  130. ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
  131. @overload
  132. def __call__(self, other: integer[_NBit2], /) -> floating[_NBit1 | _NBit2]: ...
  133. class _UnsignedIntOp(Protocol[_NBit1]):
  134. # NOTE: `uint64 + signedinteger -> float64`
  135. @overload
  136. def __call__(self, other: bool, /) -> unsignedinteger[_NBit1]: ...
  137. @overload
  138. def __call__(
  139. self, other: int | signedinteger[Any], /
  140. ) -> Any: ...
  141. @overload
  142. def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ...
  143. @overload
  144. def __call__(
  145. self, other: complex, /,
  146. ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
  147. @overload
  148. def __call__(
  149. self, other: unsignedinteger[_NBit2], /
  150. ) -> unsignedinteger[_NBit1 | _NBit2]: ...
  151. class _UnsignedIntBitOp(Protocol[_NBit1]):
  152. @overload
  153. def __call__(self, other: bool, /) -> unsignedinteger[_NBit1]: ...
  154. @overload
  155. def __call__(self, other: int, /) -> signedinteger[Any]: ...
  156. @overload
  157. def __call__(self, other: signedinteger[Any], /) -> signedinteger[Any]: ...
  158. @overload
  159. def __call__(
  160. self, other: unsignedinteger[_NBit2], /
  161. ) -> unsignedinteger[_NBit1 | _NBit2]: ...
  162. class _UnsignedIntMod(Protocol[_NBit1]):
  163. @overload
  164. def __call__(self, other: bool, /) -> unsignedinteger[_NBit1]: ...
  165. @overload
  166. def __call__(
  167. self, other: int | signedinteger[Any], /
  168. ) -> Any: ...
  169. @overload
  170. def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ...
  171. @overload
  172. def __call__(
  173. self, other: unsignedinteger[_NBit2], /
  174. ) -> unsignedinteger[_NBit1 | _NBit2]: ...
  175. class _UnsignedIntDivMod(Protocol[_NBit1]):
  176. @overload
  177. def __call__(self, other: bool, /) -> _2Tuple[signedinteger[_NBit1]]: ...
  178. @overload
  179. def __call__(
  180. self, other: int | signedinteger[Any], /
  181. ) -> _2Tuple[Any]: ...
  182. @overload
  183. def __call__(self, other: float, /) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ...
  184. @overload
  185. def __call__(
  186. self, other: unsignedinteger[_NBit2], /
  187. ) -> _2Tuple[unsignedinteger[_NBit1 | _NBit2]]: ...
  188. class _SignedIntOp(Protocol[_NBit1]):
  189. @overload
  190. def __call__(self, other: bool, /) -> signedinteger[_NBit1]: ...
  191. @overload
  192. def __call__(self, other: int, /) -> signedinteger[_NBit1 | _NBitInt]: ...
  193. @overload
  194. def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ...
  195. @overload
  196. def __call__(
  197. self, other: complex, /,
  198. ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
  199. @overload
  200. def __call__(
  201. self, other: signedinteger[_NBit2], /,
  202. ) -> signedinteger[_NBit1 | _NBit2]: ...
  203. class _SignedIntBitOp(Protocol[_NBit1]):
  204. @overload
  205. def __call__(self, other: bool, /) -> signedinteger[_NBit1]: ...
  206. @overload
  207. def __call__(self, other: int, /) -> signedinteger[_NBit1 | _NBitInt]: ...
  208. @overload
  209. def __call__(
  210. self, other: signedinteger[_NBit2], /,
  211. ) -> signedinteger[_NBit1 | _NBit2]: ...
  212. class _SignedIntMod(Protocol[_NBit1]):
  213. @overload
  214. def __call__(self, other: bool, /) -> signedinteger[_NBit1]: ...
  215. @overload
  216. def __call__(self, other: int, /) -> signedinteger[_NBit1 | _NBitInt]: ...
  217. @overload
  218. def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ...
  219. @overload
  220. def __call__(
  221. self, other: signedinteger[_NBit2], /,
  222. ) -> signedinteger[_NBit1 | _NBit2]: ...
  223. class _SignedIntDivMod(Protocol[_NBit1]):
  224. @overload
  225. def __call__(self, other: bool, /) -> _2Tuple[signedinteger[_NBit1]]: ...
  226. @overload
  227. def __call__(self, other: int, /) -> _2Tuple[signedinteger[_NBit1 | _NBitInt]]: ...
  228. @overload
  229. def __call__(self, other: float, /) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ...
  230. @overload
  231. def __call__(
  232. self, other: signedinteger[_NBit2], /,
  233. ) -> _2Tuple[signedinteger[_NBit1 | _NBit2]]: ...
  234. class _FloatOp(Protocol[_NBit1]):
  235. @overload
  236. def __call__(self, other: bool, /) -> floating[_NBit1]: ...
  237. @overload
  238. def __call__(self, other: int, /) -> floating[_NBit1 | _NBitInt]: ...
  239. @overload
  240. def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ...
  241. @overload
  242. def __call__(
  243. self, other: complex, /,
  244. ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
  245. @overload
  246. def __call__(
  247. self, other: integer[_NBit2] | floating[_NBit2], /
  248. ) -> floating[_NBit1 | _NBit2]: ...
  249. class _FloatMod(Protocol[_NBit1]):
  250. @overload
  251. def __call__(self, other: bool, /) -> floating[_NBit1]: ...
  252. @overload
  253. def __call__(self, other: int, /) -> floating[_NBit1 | _NBitInt]: ...
  254. @overload
  255. def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ...
  256. @overload
  257. def __call__(
  258. self, other: integer[_NBit2] | floating[_NBit2], /
  259. ) -> floating[_NBit1 | _NBit2]: ...
  260. class _FloatDivMod(Protocol[_NBit1]):
  261. @overload
  262. def __call__(self, other: bool, /) -> _2Tuple[floating[_NBit1]]: ...
  263. @overload
  264. def __call__(self, other: int, /) -> _2Tuple[floating[_NBit1 | _NBitInt]]: ...
  265. @overload
  266. def __call__(self, other: float, /) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ...
  267. @overload
  268. def __call__(
  269. self, other: integer[_NBit2] | floating[_NBit2], /
  270. ) -> _2Tuple[floating[_NBit1 | _NBit2]]: ...
  271. class _ComplexOp(Protocol[_NBit1]):
  272. @overload
  273. def __call__(self, other: bool, /) -> complexfloating[_NBit1, _NBit1]: ...
  274. @overload
  275. def __call__(self, other: int, /) -> complexfloating[_NBit1 | _NBitInt, _NBit1 | _NBitInt]: ...
  276. @overload
  277. def __call__(
  278. self, other: complex, /,
  279. ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
  280. @overload
  281. def __call__(
  282. self,
  283. other: (
  284. integer[_NBit2]
  285. | floating[_NBit2]
  286. | complexfloating[_NBit2, _NBit2]
  287. ), /,
  288. ) -> complexfloating[_NBit1 | _NBit2, _NBit1 | _NBit2]: ...
  289. class _NumberOp(Protocol):
  290. def __call__(self, other: _NumberLike_co, /) -> Any: ...
  291. class _SupportsLT(Protocol):
  292. def __lt__(self, other: Any, /) -> object: ...
  293. class _SupportsGT(Protocol):
  294. def __gt__(self, other: Any, /) -> object: ...
  295. class _ComparisonOp(Protocol[_T1_contra, _T2_contra]):
  296. @overload
  297. def __call__(self, other: _T1_contra, /) -> bool_: ...
  298. @overload
  299. def __call__(self, other: _T2_contra, /) -> NDArray[bool_]: ...
  300. @overload
  301. def __call__(
  302. self,
  303. other: _SupportsLT | _SupportsGT | _NestedSequence[_SupportsLT | _SupportsGT],
  304. /,
  305. ) -> Any: ...