_callable.pyi 12 KB

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