_ufunc.pyi 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. """A module with private type-check-only `numpy.ufunc` subclasses.
  2. The signatures of the ufuncs are too varied to reasonably type
  3. with a single class. So instead, `ufunc` has been expanded into
  4. four private subclasses, one for each combination of
  5. `~ufunc.nin` and `~ufunc.nout`.
  6. """
  7. from typing import (
  8. Any,
  9. Generic,
  10. overload,
  11. TypeVar,
  12. Literal,
  13. SupportsIndex,
  14. Protocol,
  15. )
  16. from numpy import ufunc, _CastingKind, _OrderKACF
  17. from numpy.typing import NDArray
  18. from ._shape import _ShapeLike
  19. from ._scalars import _ScalarLike_co
  20. from ._array_like import ArrayLike, _ArrayLikeBool_co, _ArrayLikeInt_co
  21. from ._dtype_like import DTypeLike
  22. _T = TypeVar("_T")
  23. _2Tuple = tuple[_T, _T]
  24. _3Tuple = tuple[_T, _T, _T]
  25. _4Tuple = tuple[_T, _T, _T, _T]
  26. _NTypes = TypeVar("_NTypes", bound=int)
  27. _IDType = TypeVar("_IDType", bound=Any)
  28. _NameType = TypeVar("_NameType", bound=str)
  29. class _SupportsArrayUFunc(Protocol):
  30. def __array_ufunc__(
  31. self,
  32. ufunc: ufunc,
  33. method: Literal["__call__", "reduce", "reduceat", "accumulate", "outer", "inner"],
  34. *inputs: Any,
  35. **kwargs: Any,
  36. ) -> Any: ...
  37. # NOTE: In reality `extobj` should be a length of list 3 containing an
  38. # int, an int, and a callable, but there's no way to properly express
  39. # non-homogenous lists.
  40. # Use `Any` over `Union` to avoid issues related to lists invariance.
  41. # NOTE: `reduce`, `accumulate`, `reduceat` and `outer` raise a ValueError for
  42. # ufuncs that don't accept two input arguments and return one output argument.
  43. # In such cases the respective methods are simply typed as `None`.
  44. # NOTE: Similarly, `at` won't be defined for ufuncs that return
  45. # multiple outputs; in such cases `at` is typed as `None`
  46. # NOTE: If 2 output types are returned then `out` must be a
  47. # 2-tuple of arrays. Otherwise `None` or a plain array are also acceptable
  48. class _UFunc_Nin1_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]): # type: ignore[misc]
  49. @property
  50. def __name__(self) -> _NameType: ...
  51. @property
  52. def ntypes(self) -> _NTypes: ...
  53. @property
  54. def identity(self) -> _IDType: ...
  55. @property
  56. def nin(self) -> Literal[1]: ...
  57. @property
  58. def nout(self) -> Literal[1]: ...
  59. @property
  60. def nargs(self) -> Literal[2]: ...
  61. @property
  62. def signature(self) -> None: ...
  63. @property
  64. def reduce(self) -> None: ...
  65. @property
  66. def accumulate(self) -> None: ...
  67. @property
  68. def reduceat(self) -> None: ...
  69. @property
  70. def outer(self) -> None: ...
  71. @overload
  72. def __call__(
  73. self,
  74. __x1: _ScalarLike_co,
  75. out: None = ...,
  76. *,
  77. where: None | _ArrayLikeBool_co = ...,
  78. casting: _CastingKind = ...,
  79. order: _OrderKACF = ...,
  80. dtype: DTypeLike = ...,
  81. subok: bool = ...,
  82. signature: str | _2Tuple[None | str] = ...,
  83. extobj: list[Any] = ...,
  84. ) -> Any: ...
  85. @overload
  86. def __call__(
  87. self,
  88. __x1: ArrayLike,
  89. out: None | NDArray[Any] | tuple[NDArray[Any]] = ...,
  90. *,
  91. where: None | _ArrayLikeBool_co = ...,
  92. casting: _CastingKind = ...,
  93. order: _OrderKACF = ...,
  94. dtype: DTypeLike = ...,
  95. subok: bool = ...,
  96. signature: str | _2Tuple[None | str] = ...,
  97. extobj: list[Any] = ...,
  98. ) -> NDArray[Any]: ...
  99. @overload
  100. def __call__(
  101. self,
  102. __x1: _SupportsArrayUFunc,
  103. out: None | NDArray[Any] | tuple[NDArray[Any]] = ...,
  104. *,
  105. where: None | _ArrayLikeBool_co = ...,
  106. casting: _CastingKind = ...,
  107. order: _OrderKACF = ...,
  108. dtype: DTypeLike = ...,
  109. subok: bool = ...,
  110. signature: str | _2Tuple[None | str] = ...,
  111. extobj: list[Any] = ...,
  112. ) -> Any: ...
  113. def at(
  114. self,
  115. a: _SupportsArrayUFunc,
  116. indices: _ArrayLikeInt_co,
  117. /,
  118. ) -> None: ...
  119. class _UFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]): # type: ignore[misc]
  120. @property
  121. def __name__(self) -> _NameType: ...
  122. @property
  123. def ntypes(self) -> _NTypes: ...
  124. @property
  125. def identity(self) -> _IDType: ...
  126. @property
  127. def nin(self) -> Literal[2]: ...
  128. @property
  129. def nout(self) -> Literal[1]: ...
  130. @property
  131. def nargs(self) -> Literal[3]: ...
  132. @property
  133. def signature(self) -> None: ...
  134. @overload
  135. def __call__(
  136. self,
  137. __x1: _ScalarLike_co,
  138. __x2: _ScalarLike_co,
  139. out: None = ...,
  140. *,
  141. where: None | _ArrayLikeBool_co = ...,
  142. casting: _CastingKind = ...,
  143. order: _OrderKACF = ...,
  144. dtype: DTypeLike = ...,
  145. subok: bool = ...,
  146. signature: str | _3Tuple[None | str] = ...,
  147. extobj: list[Any] = ...,
  148. ) -> Any: ...
  149. @overload
  150. def __call__(
  151. self,
  152. __x1: ArrayLike,
  153. __x2: ArrayLike,
  154. out: None | NDArray[Any] | tuple[NDArray[Any]] = ...,
  155. *,
  156. where: None | _ArrayLikeBool_co = ...,
  157. casting: _CastingKind = ...,
  158. order: _OrderKACF = ...,
  159. dtype: DTypeLike = ...,
  160. subok: bool = ...,
  161. signature: str | _3Tuple[None | str] = ...,
  162. extobj: list[Any] = ...,
  163. ) -> NDArray[Any]: ...
  164. def at(
  165. self,
  166. a: NDArray[Any],
  167. indices: _ArrayLikeInt_co,
  168. b: ArrayLike,
  169. /,
  170. ) -> None: ...
  171. def reduce(
  172. self,
  173. array: ArrayLike,
  174. axis: None | _ShapeLike = ...,
  175. dtype: DTypeLike = ...,
  176. out: None | NDArray[Any] = ...,
  177. keepdims: bool = ...,
  178. initial: Any = ...,
  179. where: _ArrayLikeBool_co = ...,
  180. ) -> Any: ...
  181. def accumulate(
  182. self,
  183. array: ArrayLike,
  184. axis: SupportsIndex = ...,
  185. dtype: DTypeLike = ...,
  186. out: None | NDArray[Any] = ...,
  187. ) -> NDArray[Any]: ...
  188. def reduceat(
  189. self,
  190. array: ArrayLike,
  191. indices: _ArrayLikeInt_co,
  192. axis: SupportsIndex = ...,
  193. dtype: DTypeLike = ...,
  194. out: None | NDArray[Any] = ...,
  195. ) -> NDArray[Any]: ...
  196. # Expand `**kwargs` into explicit keyword-only arguments
  197. @overload
  198. def outer(
  199. self,
  200. A: _ScalarLike_co,
  201. B: _ScalarLike_co,
  202. /, *,
  203. out: None = ...,
  204. where: None | _ArrayLikeBool_co = ...,
  205. casting: _CastingKind = ...,
  206. order: _OrderKACF = ...,
  207. dtype: DTypeLike = ...,
  208. subok: bool = ...,
  209. signature: str | _3Tuple[None | str] = ...,
  210. extobj: list[Any] = ...,
  211. ) -> Any: ...
  212. @overload
  213. def outer( # type: ignore[misc]
  214. self,
  215. A: ArrayLike,
  216. B: ArrayLike,
  217. /, *,
  218. out: None | NDArray[Any] | tuple[NDArray[Any]] = ...,
  219. where: None | _ArrayLikeBool_co = ...,
  220. casting: _CastingKind = ...,
  221. order: _OrderKACF = ...,
  222. dtype: DTypeLike = ...,
  223. subok: bool = ...,
  224. signature: str | _3Tuple[None | str] = ...,
  225. extobj: list[Any] = ...,
  226. ) -> NDArray[Any]: ...
  227. class _UFunc_Nin1_Nout2(ufunc, Generic[_NameType, _NTypes, _IDType]): # type: ignore[misc]
  228. @property
  229. def __name__(self) -> _NameType: ...
  230. @property
  231. def ntypes(self) -> _NTypes: ...
  232. @property
  233. def identity(self) -> _IDType: ...
  234. @property
  235. def nin(self) -> Literal[1]: ...
  236. @property
  237. def nout(self) -> Literal[2]: ...
  238. @property
  239. def nargs(self) -> Literal[3]: ...
  240. @property
  241. def signature(self) -> None: ...
  242. @property
  243. def at(self) -> None: ...
  244. @property
  245. def reduce(self) -> None: ...
  246. @property
  247. def accumulate(self) -> None: ...
  248. @property
  249. def reduceat(self) -> None: ...
  250. @property
  251. def outer(self) -> None: ...
  252. @overload
  253. def __call__(
  254. self,
  255. __x1: _ScalarLike_co,
  256. __out1: None = ...,
  257. __out2: None = ...,
  258. *,
  259. where: None | _ArrayLikeBool_co = ...,
  260. casting: _CastingKind = ...,
  261. order: _OrderKACF = ...,
  262. dtype: DTypeLike = ...,
  263. subok: bool = ...,
  264. signature: str | _3Tuple[None | str] = ...,
  265. extobj: list[Any] = ...,
  266. ) -> _2Tuple[Any]: ...
  267. @overload
  268. def __call__(
  269. self,
  270. __x1: ArrayLike,
  271. __out1: None | NDArray[Any] = ...,
  272. __out2: None | NDArray[Any] = ...,
  273. *,
  274. out: _2Tuple[NDArray[Any]] = ...,
  275. where: None | _ArrayLikeBool_co = ...,
  276. casting: _CastingKind = ...,
  277. order: _OrderKACF = ...,
  278. dtype: DTypeLike = ...,
  279. subok: bool = ...,
  280. signature: str | _3Tuple[None | str] = ...,
  281. extobj: list[Any] = ...,
  282. ) -> _2Tuple[NDArray[Any]]: ...
  283. @overload
  284. def __call__(
  285. self,
  286. __x1: _SupportsArrayUFunc,
  287. __out1: None | NDArray[Any] = ...,
  288. __out2: None | NDArray[Any] = ...,
  289. *,
  290. out: _2Tuple[NDArray[Any]] = ...,
  291. where: None | _ArrayLikeBool_co = ...,
  292. casting: _CastingKind = ...,
  293. order: _OrderKACF = ...,
  294. dtype: DTypeLike = ...,
  295. subok: bool = ...,
  296. signature: str | _3Tuple[None | str] = ...,
  297. extobj: list[Any] = ...,
  298. ) -> _2Tuple[Any]: ...
  299. class _UFunc_Nin2_Nout2(ufunc, Generic[_NameType, _NTypes, _IDType]): # type: ignore[misc]
  300. @property
  301. def __name__(self) -> _NameType: ...
  302. @property
  303. def ntypes(self) -> _NTypes: ...
  304. @property
  305. def identity(self) -> _IDType: ...
  306. @property
  307. def nin(self) -> Literal[2]: ...
  308. @property
  309. def nout(self) -> Literal[2]: ...
  310. @property
  311. def nargs(self) -> Literal[4]: ...
  312. @property
  313. def signature(self) -> None: ...
  314. @property
  315. def at(self) -> None: ...
  316. @property
  317. def reduce(self) -> None: ...
  318. @property
  319. def accumulate(self) -> None: ...
  320. @property
  321. def reduceat(self) -> None: ...
  322. @property
  323. def outer(self) -> None: ...
  324. @overload
  325. def __call__(
  326. self,
  327. __x1: _ScalarLike_co,
  328. __x2: _ScalarLike_co,
  329. __out1: None = ...,
  330. __out2: None = ...,
  331. *,
  332. where: None | _ArrayLikeBool_co = ...,
  333. casting: _CastingKind = ...,
  334. order: _OrderKACF = ...,
  335. dtype: DTypeLike = ...,
  336. subok: bool = ...,
  337. signature: str | _4Tuple[None | str] = ...,
  338. extobj: list[Any] = ...,
  339. ) -> _2Tuple[Any]: ...
  340. @overload
  341. def __call__(
  342. self,
  343. __x1: ArrayLike,
  344. __x2: ArrayLike,
  345. __out1: None | NDArray[Any] = ...,
  346. __out2: None | NDArray[Any] = ...,
  347. *,
  348. out: _2Tuple[NDArray[Any]] = ...,
  349. where: None | _ArrayLikeBool_co = ...,
  350. casting: _CastingKind = ...,
  351. order: _OrderKACF = ...,
  352. dtype: DTypeLike = ...,
  353. subok: bool = ...,
  354. signature: str | _4Tuple[None | str] = ...,
  355. extobj: list[Any] = ...,
  356. ) -> _2Tuple[NDArray[Any]]: ...
  357. class _GUFunc_Nin2_Nout1(ufunc, Generic[_NameType, _NTypes, _IDType]): # type: ignore[misc]
  358. @property
  359. def __name__(self) -> _NameType: ...
  360. @property
  361. def ntypes(self) -> _NTypes: ...
  362. @property
  363. def identity(self) -> _IDType: ...
  364. @property
  365. def nin(self) -> Literal[2]: ...
  366. @property
  367. def nout(self) -> Literal[1]: ...
  368. @property
  369. def nargs(self) -> Literal[3]: ...
  370. # NOTE: In practice the only gufunc in the main namespace is `matmul`,
  371. # so we can use its signature here
  372. @property
  373. def signature(self) -> Literal["(n?,k),(k,m?)->(n?,m?)"]: ...
  374. @property
  375. def reduce(self) -> None: ...
  376. @property
  377. def accumulate(self) -> None: ...
  378. @property
  379. def reduceat(self) -> None: ...
  380. @property
  381. def outer(self) -> None: ...
  382. @property
  383. def at(self) -> None: ...
  384. # Scalar for 1D array-likes; ndarray otherwise
  385. @overload
  386. def __call__(
  387. self,
  388. __x1: ArrayLike,
  389. __x2: ArrayLike,
  390. out: None = ...,
  391. *,
  392. casting: _CastingKind = ...,
  393. order: _OrderKACF = ...,
  394. dtype: DTypeLike = ...,
  395. subok: bool = ...,
  396. signature: str | _3Tuple[None | str] = ...,
  397. extobj: list[Any] = ...,
  398. axes: list[_2Tuple[SupportsIndex]] = ...,
  399. ) -> Any: ...
  400. @overload
  401. def __call__(
  402. self,
  403. __x1: ArrayLike,
  404. __x2: ArrayLike,
  405. out: NDArray[Any] | tuple[NDArray[Any]],
  406. *,
  407. casting: _CastingKind = ...,
  408. order: _OrderKACF = ...,
  409. dtype: DTypeLike = ...,
  410. subok: bool = ...,
  411. signature: str | _3Tuple[None | str] = ...,
  412. extobj: list[Any] = ...,
  413. axes: list[_2Tuple[SupportsIndex]] = ...,
  414. ) -> NDArray[Any]: ...