_twodim_base_impl.pyi 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. from _typeshed import Incomplete
  2. from collections.abc import Callable, Sequence
  3. from typing import (
  4. Any,
  5. Literal as L,
  6. Never,
  7. Protocol,
  8. TypeAlias,
  9. TypeVar,
  10. overload,
  11. type_check_only,
  12. )
  13. import numpy as np
  14. from numpy import _OrderCF
  15. from numpy._typing import (
  16. ArrayLike,
  17. DTypeLike,
  18. NDArray,
  19. _ArrayLike,
  20. _DTypeLike,
  21. _NumberLike_co,
  22. _ScalarLike_co,
  23. _SupportsArray,
  24. _SupportsArrayFunc,
  25. )
  26. __all__ = [
  27. "diag",
  28. "diagflat",
  29. "eye",
  30. "fliplr",
  31. "flipud",
  32. "tri",
  33. "triu",
  34. "tril",
  35. "vander",
  36. "histogram2d",
  37. "mask_indices",
  38. "tril_indices",
  39. "tril_indices_from",
  40. "triu_indices",
  41. "triu_indices_from",
  42. ]
  43. ###
  44. _T = TypeVar("_T")
  45. _ArrayT = TypeVar("_ArrayT", bound=np.ndarray)
  46. _ScalarT = TypeVar("_ScalarT", bound=np.generic)
  47. _ComplexT = TypeVar("_ComplexT", bound=np.complexfloating)
  48. _InexactT = TypeVar("_InexactT", bound=np.inexact)
  49. _NumberT = TypeVar("_NumberT", bound=np.number)
  50. _NumberObjectT = TypeVar("_NumberObjectT", bound=np.number | np.object_)
  51. _NumberCoT = TypeVar("_NumberCoT", bound=_Number_co)
  52. _Int_co: TypeAlias = np.integer | np.bool
  53. _Float_co: TypeAlias = np.floating | _Int_co
  54. _Number_co: TypeAlias = np.number | np.bool
  55. _Array1D: TypeAlias = np.ndarray[tuple[int], np.dtype[_ScalarT]]
  56. _Array2D: TypeAlias = np.ndarray[tuple[int, int], np.dtype[_ScalarT]]
  57. # Workaround for mypy's and pyright's lack of compliance with the typing spec for
  58. # overloads for gradual types. This works because only `Any` and `Never` are assignable
  59. # to `Never`.
  60. _ArrayNoD: TypeAlias = np.ndarray[tuple[Never] | tuple[Never, Never], np.dtype[_ScalarT]]
  61. _ArrayLike1D: TypeAlias = _SupportsArray[np.dtype[_ScalarT]] | Sequence[_ScalarT]
  62. _ArrayLike1DInt_co: TypeAlias = _SupportsArray[np.dtype[_Int_co]] | Sequence[int | _Int_co]
  63. _ArrayLike1DFloat_co: TypeAlias = _SupportsArray[np.dtype[_Float_co]] | Sequence[float | _Float_co]
  64. _ArrayLike2DFloat_co: TypeAlias = _SupportsArray[np.dtype[_Float_co]] | Sequence[_ArrayLike1DFloat_co]
  65. _ArrayLike1DNumber_co: TypeAlias = _SupportsArray[np.dtype[_Number_co]] | Sequence[complex | _Number_co]
  66. # The returned arrays dtype must be compatible with `np.equal`
  67. _MaskFunc: TypeAlias = Callable[[NDArray[np.int_], _T], NDArray[_Number_co | np.timedelta64 | np.datetime64 | np.object_]]
  68. _Indices2D: TypeAlias = tuple[_Array1D[np.intp], _Array1D[np.intp]]
  69. _Histogram2D: TypeAlias = tuple[_Array2D[np.float64], _Array1D[_ScalarT], _Array1D[_ScalarT]]
  70. @type_check_only
  71. class _HasShapeAndNDim(Protocol):
  72. @property # TODO: require 2d shape once shape-typing has matured
  73. def shape(self) -> tuple[int, ...]: ...
  74. @property
  75. def ndim(self) -> int: ...
  76. ###
  77. # keep in sync with `flipud`
  78. @overload
  79. def fliplr(m: _ArrayT) -> _ArrayT: ...
  80. @overload
  81. def fliplr(m: _ArrayLike[_ScalarT]) -> NDArray[_ScalarT]: ...
  82. @overload
  83. def fliplr(m: ArrayLike) -> NDArray[Any]: ...
  84. # keep in sync with `fliplr`
  85. @overload
  86. def flipud(m: _ArrayT) -> _ArrayT: ...
  87. @overload
  88. def flipud(m: _ArrayLike[_ScalarT]) -> NDArray[_ScalarT]: ...
  89. @overload
  90. def flipud(m: ArrayLike) -> NDArray[Any]: ...
  91. #
  92. @overload
  93. def eye(
  94. N: int,
  95. M: int | None = None,
  96. k: int = 0,
  97. dtype: None = ..., # = float # stubdefaulter: ignore[missing-default]
  98. order: _OrderCF = "C",
  99. *,
  100. device: L["cpu"] | None = None,
  101. like: _SupportsArrayFunc | None = None,
  102. ) -> _Array2D[np.float64]: ...
  103. @overload
  104. def eye(
  105. N: int,
  106. M: int | None,
  107. k: int,
  108. dtype: _DTypeLike[_ScalarT],
  109. order: _OrderCF = "C",
  110. *,
  111. device: L["cpu"] | None = None,
  112. like: _SupportsArrayFunc | None = None,
  113. ) -> _Array2D[_ScalarT]: ...
  114. @overload
  115. def eye(
  116. N: int,
  117. M: int | None = None,
  118. k: int = 0,
  119. *,
  120. dtype: _DTypeLike[_ScalarT],
  121. order: _OrderCF = "C",
  122. device: L["cpu"] | None = None,
  123. like: _SupportsArrayFunc | None = None,
  124. ) -> _Array2D[_ScalarT]: ...
  125. @overload
  126. def eye(
  127. N: int,
  128. M: int | None = None,
  129. k: int = 0,
  130. dtype: DTypeLike | None = ..., # = float
  131. order: _OrderCF = "C",
  132. *,
  133. device: L["cpu"] | None = None,
  134. like: _SupportsArrayFunc | None = None,
  135. ) -> _Array2D[Incomplete]: ...
  136. #
  137. @overload
  138. def diag(v: _ArrayNoD[_ScalarT] | Sequence[Sequence[_ScalarT]], k: int = 0) -> NDArray[_ScalarT]: ...
  139. @overload
  140. def diag(v: _Array2D[_ScalarT] | Sequence[Sequence[_ScalarT]], k: int = 0) -> _Array1D[_ScalarT]: ...
  141. @overload
  142. def diag(v: _Array1D[_ScalarT] | Sequence[_ScalarT], k: int = 0) -> _Array2D[_ScalarT]: ...
  143. @overload
  144. def diag(v: Sequence[Sequence[_ScalarLike_co]], k: int = 0) -> _Array1D[Incomplete]: ...
  145. @overload
  146. def diag(v: Sequence[_ScalarLike_co], k: int = 0) -> _Array2D[Incomplete]: ...
  147. @overload
  148. def diag(v: _ArrayLike[_ScalarT], k: int = 0) -> NDArray[_ScalarT]: ...
  149. @overload
  150. def diag(v: ArrayLike, k: int = 0) -> NDArray[Incomplete]: ...
  151. # keep in sync with `numpy.ma.extras.diagflat`
  152. @overload
  153. def diagflat(v: _ArrayLike[_ScalarT], k: int = 0) -> _Array2D[_ScalarT]: ...
  154. @overload
  155. def diagflat(v: ArrayLike, k: int = 0) -> _Array2D[Incomplete]: ...
  156. #
  157. @overload
  158. def tri(
  159. N: int,
  160. M: int | None = None,
  161. k: int = 0,
  162. dtype: None = ..., # = float # stubdefaulter: ignore[missing-default]
  163. *,
  164. like: _SupportsArrayFunc | None = None
  165. ) -> _Array2D[np.float64]: ...
  166. @overload
  167. def tri(
  168. N: int,
  169. M: int | None,
  170. k: int,
  171. dtype: _DTypeLike[_ScalarT],
  172. *,
  173. like: _SupportsArrayFunc | None = None
  174. ) -> _Array2D[_ScalarT]: ...
  175. @overload
  176. def tri(
  177. N: int,
  178. M: int | None = None,
  179. k: int = 0,
  180. *,
  181. dtype: _DTypeLike[_ScalarT],
  182. like: _SupportsArrayFunc | None = None
  183. ) -> _Array2D[_ScalarT]: ...
  184. @overload
  185. def tri(
  186. N: int,
  187. M: int | None = None,
  188. k: int = 0,
  189. dtype: DTypeLike | None = ..., # = float
  190. *,
  191. like: _SupportsArrayFunc | None = None
  192. ) -> _Array2D[Any]: ...
  193. # keep in sync with `triu`
  194. @overload
  195. def tril(m: _ArrayT, k: int = 0) -> _ArrayT: ...
  196. @overload
  197. def tril(m: _ArrayLike[_ScalarT], k: int = 0) -> NDArray[_ScalarT]: ...
  198. @overload
  199. def tril(m: ArrayLike, k: int = 0) -> NDArray[Any]: ...
  200. # keep in sync with `tril`
  201. @overload
  202. def triu(m: _ArrayT, k: int = 0) -> _ArrayT: ...
  203. @overload
  204. def triu(m: _ArrayLike[_ScalarT], k: int = 0) -> NDArray[_ScalarT]: ...
  205. @overload
  206. def triu(m: ArrayLike, k: int = 0) -> NDArray[Any]: ...
  207. # we use `list` (invariant) instead of `Sequence` (covariant) to avoid overlap
  208. @overload
  209. def vander(x: _ArrayLike1D[_NumberObjectT], N: int | None = None, increasing: bool = False) -> _Array2D[_NumberObjectT]: ...
  210. @overload
  211. def vander(x: _ArrayLike1D[np.bool] | list[int], N: int | None = None, increasing: bool = False) -> _Array2D[np.int_]: ...
  212. @overload
  213. def vander(x: list[float], N: int | None = None, increasing: bool = False) -> _Array2D[np.float64]: ...
  214. @overload
  215. def vander(x: list[complex], N: int | None = None, increasing: bool = False) -> _Array2D[np.complex128]: ...
  216. @overload # fallback
  217. def vander(x: Sequence[_NumberLike_co], N: int | None = None, increasing: bool = False) -> _Array2D[Any]: ...
  218. #
  219. @overload
  220. def histogram2d(
  221. x: _ArrayLike1D[_ComplexT],
  222. y: _ArrayLike1D[_ComplexT | _Float_co],
  223. bins: int | Sequence[int] = 10,
  224. range: _ArrayLike2DFloat_co | None = None,
  225. density: bool | None = None,
  226. weights: _ArrayLike1DFloat_co | None = None,
  227. ) -> _Histogram2D[_ComplexT]: ...
  228. @overload
  229. def histogram2d(
  230. x: _ArrayLike1D[_ComplexT | _Float_co],
  231. y: _ArrayLike1D[_ComplexT],
  232. bins: int | Sequence[int] = 10,
  233. range: _ArrayLike2DFloat_co | None = None,
  234. density: bool | None = None,
  235. weights: _ArrayLike1DFloat_co | None = None,
  236. ) -> _Histogram2D[_ComplexT]: ...
  237. @overload
  238. def histogram2d(
  239. x: _ArrayLike1D[_InexactT],
  240. y: _ArrayLike1D[_InexactT | _Int_co],
  241. bins: int | Sequence[int] = 10,
  242. range: _ArrayLike2DFloat_co | None = None,
  243. density: bool | None = None,
  244. weights: _ArrayLike1DFloat_co | None = None,
  245. ) -> _Histogram2D[_InexactT]: ...
  246. @overload
  247. def histogram2d(
  248. x: _ArrayLike1D[_InexactT | _Int_co],
  249. y: _ArrayLike1D[_InexactT],
  250. bins: int | Sequence[int] = 10,
  251. range: _ArrayLike2DFloat_co | None = None,
  252. density: bool | None = None,
  253. weights: _ArrayLike1DFloat_co | None = None,
  254. ) -> _Histogram2D[_InexactT]: ...
  255. @overload
  256. def histogram2d(
  257. x: _ArrayLike1DInt_co | Sequence[float],
  258. y: _ArrayLike1DInt_co | Sequence[float],
  259. bins: int | Sequence[int] = 10,
  260. range: _ArrayLike2DFloat_co | None = None,
  261. density: bool | None = None,
  262. weights: _ArrayLike1DFloat_co | None = None,
  263. ) -> _Histogram2D[np.float64]: ...
  264. @overload
  265. def histogram2d(
  266. x: Sequence[complex],
  267. y: Sequence[complex],
  268. bins: int | Sequence[int] = 10,
  269. range: _ArrayLike2DFloat_co | None = None,
  270. density: bool | None = None,
  271. weights: _ArrayLike1DFloat_co | None = None,
  272. ) -> _Histogram2D[np.complex128 | Any]: ...
  273. @overload
  274. def histogram2d(
  275. x: _ArrayLike1DNumber_co,
  276. y: _ArrayLike1DNumber_co,
  277. bins: _ArrayLike1D[_NumberCoT] | Sequence[_ArrayLike1D[_NumberCoT]],
  278. range: _ArrayLike2DFloat_co | None = None,
  279. density: bool | None = None,
  280. weights: _ArrayLike1DFloat_co | None = None,
  281. ) -> _Histogram2D[_NumberCoT]: ...
  282. @overload
  283. def histogram2d(
  284. x: _ArrayLike1D[_InexactT],
  285. y: _ArrayLike1D[_InexactT],
  286. bins: Sequence[_ArrayLike1D[_NumberCoT] | int],
  287. range: _ArrayLike2DFloat_co | None = None,
  288. density: bool | None = None,
  289. weights: _ArrayLike1DFloat_co | None = None,
  290. ) -> _Histogram2D[_InexactT | _NumberCoT]: ...
  291. @overload
  292. def histogram2d(
  293. x: _ArrayLike1D[_InexactT],
  294. y: _ArrayLike1D[_InexactT],
  295. bins: Sequence[_ArrayLike1DNumber_co | int],
  296. range: _ArrayLike2DFloat_co | None = None,
  297. density: bool | None = None,
  298. weights: _ArrayLike1DFloat_co | None = None,
  299. ) -> _Histogram2D[_InexactT | Any]: ...
  300. @overload
  301. def histogram2d(
  302. x: _ArrayLike1DInt_co | Sequence[float],
  303. y: _ArrayLike1DInt_co | Sequence[float],
  304. bins: Sequence[_ArrayLike1D[_NumberCoT] | int],
  305. range: _ArrayLike2DFloat_co | None = None,
  306. density: bool | None = None,
  307. weights: _ArrayLike1DFloat_co | None = None,
  308. ) -> _Histogram2D[np.float64 | _NumberCoT]: ...
  309. @overload
  310. def histogram2d(
  311. x: _ArrayLike1DInt_co | Sequence[float],
  312. y: _ArrayLike1DInt_co | Sequence[float],
  313. bins: Sequence[_ArrayLike1DNumber_co | int],
  314. range: _ArrayLike2DFloat_co | None = None,
  315. density: bool | None = None,
  316. weights: _ArrayLike1DFloat_co | None = None,
  317. ) -> _Histogram2D[np.float64 | Any]: ...
  318. @overload
  319. def histogram2d(
  320. x: Sequence[complex],
  321. y: Sequence[complex],
  322. bins: Sequence[_ArrayLike1D[_NumberCoT] | int],
  323. range: _ArrayLike2DFloat_co | None = None,
  324. density: bool | None = None,
  325. weights: _ArrayLike1DFloat_co | None = None,
  326. ) -> _Histogram2D[np.complex128 | _NumberCoT]: ...
  327. @overload
  328. def histogram2d(
  329. x: Sequence[complex],
  330. y: Sequence[complex],
  331. bins: Sequence[_ArrayLike1DNumber_co | int],
  332. range: _ArrayLike2DFloat_co | None = None,
  333. density: bool | None = None,
  334. weights: _ArrayLike1DFloat_co | None = None,
  335. ) -> _Histogram2D[np.complex128 | Any]: ...
  336. @overload
  337. def histogram2d(
  338. x: _ArrayLike1DNumber_co,
  339. y: _ArrayLike1DNumber_co,
  340. bins: Sequence[Sequence[int]],
  341. range: _ArrayLike2DFloat_co | None = None,
  342. density: bool | None = None,
  343. weights: _ArrayLike1DFloat_co | None = None,
  344. ) -> _Histogram2D[np.int_]: ...
  345. @overload
  346. def histogram2d(
  347. x: _ArrayLike1DNumber_co,
  348. y: _ArrayLike1DNumber_co,
  349. bins: Sequence[Sequence[float]],
  350. range: _ArrayLike2DFloat_co | None = None,
  351. density: bool | None = None,
  352. weights: _ArrayLike1DFloat_co | None = None,
  353. ) -> _Histogram2D[np.float64 | Any]: ...
  354. @overload
  355. def histogram2d(
  356. x: _ArrayLike1DNumber_co,
  357. y: _ArrayLike1DNumber_co,
  358. bins: Sequence[Sequence[complex]],
  359. range: _ArrayLike2DFloat_co | None = None,
  360. density: bool | None = None,
  361. weights: _ArrayLike1DFloat_co | None = None,
  362. ) -> _Histogram2D[np.complex128 | Any]: ...
  363. @overload
  364. def histogram2d(
  365. x: _ArrayLike1DNumber_co,
  366. y: _ArrayLike1DNumber_co,
  367. bins: Sequence[_ArrayLike1DNumber_co | int] | int,
  368. range: _ArrayLike2DFloat_co | None = None,
  369. density: bool | None = None,
  370. weights: _ArrayLike1DFloat_co | None = None,
  371. ) -> _Histogram2D[Any]: ...
  372. # NOTE: we're assuming/demanding here the `mask_func` returns
  373. # an ndarray of shape `(n, n)`; otherwise there is the possibility
  374. # of the output tuple having more or less than 2 elements
  375. @overload
  376. def mask_indices(n: int, mask_func: _MaskFunc[int], k: int = 0) -> _Indices2D: ...
  377. @overload
  378. def mask_indices(n: int, mask_func: _MaskFunc[_T], k: _T) -> _Indices2D: ...
  379. #
  380. def tril_indices(n: int, k: int = 0, m: int | None = None) -> _Indices2D: ...
  381. def triu_indices(n: int, k: int = 0, m: int | None = None) -> _Indices2D: ...
  382. # these will accept anything with `shape: tuple[int, int]` and `ndim: int` attributes
  383. def tril_indices_from(arr: _HasShapeAndNDim, k: int = 0) -> _Indices2D: ...
  384. def triu_indices_from(arr: _HasShapeAndNDim, k: int = 0) -> _Indices2D: ...