_callable.py 11 KB

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