_index_tricks_impl.pyi 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. from _typeshed import Incomplete, SupportsLenAndGetItem
  2. from collections.abc import Sequence
  3. from typing import (
  4. Any,
  5. ClassVar,
  6. Final,
  7. Generic,
  8. Literal as L,
  9. Self,
  10. SupportsIndex,
  11. final,
  12. overload,
  13. )
  14. from typing_extensions import TypeVar
  15. import numpy as np
  16. from numpy import _CastingKind
  17. from numpy._core.multiarray import ravel_multi_index, unravel_index
  18. from numpy._typing import (
  19. ArrayLike,
  20. DTypeLike,
  21. NDArray,
  22. _AnyShape,
  23. _ArrayLike,
  24. _DTypeLike,
  25. _FiniteNestedSequence,
  26. _HasDType,
  27. _NestedSequence,
  28. _SupportsArray,
  29. )
  30. __all__ = [ # noqa: RUF022
  31. "ravel_multi_index",
  32. "unravel_index",
  33. "mgrid",
  34. "ogrid",
  35. "r_",
  36. "c_",
  37. "s_",
  38. "index_exp",
  39. "ix_",
  40. "ndenumerate",
  41. "ndindex",
  42. "fill_diagonal",
  43. "diag_indices",
  44. "diag_indices_from",
  45. ]
  46. ###
  47. _T = TypeVar("_T")
  48. _TupleT = TypeVar("_TupleT", bound=tuple[Any, ...])
  49. _ArrayT = TypeVar("_ArrayT", bound=NDArray[Any])
  50. _DTypeT = TypeVar("_DTypeT", bound=np.dtype)
  51. _ScalarT = TypeVar("_ScalarT", bound=np.generic)
  52. _ScalarT_co = TypeVar("_ScalarT_co", bound=np.generic, default=Any, covariant=True)
  53. _BoolT_co = TypeVar("_BoolT_co", bound=bool, default=bool, covariant=True)
  54. _AxisT_co = TypeVar("_AxisT_co", bound=int, default=L[0], covariant=True)
  55. _MatrixT_co = TypeVar("_MatrixT_co", bound=bool, default=L[False], covariant=True)
  56. _NDMinT_co = TypeVar("_NDMinT_co", bound=int, default=L[1], covariant=True)
  57. _Trans1DT_co = TypeVar("_Trans1DT_co", bound=int, default=L[-1], covariant=True)
  58. ###
  59. class ndenumerate(Generic[_ScalarT_co]):
  60. @overload
  61. def __init__(self: ndenumerate[_ScalarT], arr: _FiniteNestedSequence[_SupportsArray[np.dtype[_ScalarT]]]) -> None: ...
  62. @overload
  63. def __init__(self: ndenumerate[np.str_], arr: str | _NestedSequence[str]) -> None: ...
  64. @overload
  65. def __init__(self: ndenumerate[np.bytes_], arr: bytes | _NestedSequence[bytes]) -> None: ...
  66. @overload
  67. def __init__(self: ndenumerate[np.bool], arr: bool | _NestedSequence[bool]) -> None: ...
  68. @overload
  69. def __init__(self: ndenumerate[np.intp], arr: int | _NestedSequence[int]) -> None: ...
  70. @overload
  71. def __init__(self: ndenumerate[np.float64], arr: float | _NestedSequence[float]) -> None: ...
  72. @overload
  73. def __init__(self: ndenumerate[np.complex128], arr: complex | _NestedSequence[complex]) -> None: ...
  74. @overload
  75. def __init__(self: ndenumerate[Incomplete], arr: object) -> None: ...
  76. # The first overload is a (semi-)workaround for a mypy bug (tested with v1.10 and v1.11)
  77. @overload
  78. def __next__(
  79. self: ndenumerate[np.bool | np.number | np.flexible | np.datetime64 | np.timedelta64],
  80. /,
  81. ) -> tuple[_AnyShape, _ScalarT_co]: ...
  82. @overload
  83. def __next__(self: ndenumerate[np.object_], /) -> tuple[_AnyShape, Incomplete]: ...
  84. @overload
  85. def __next__(self, /) -> tuple[_AnyShape, _ScalarT_co]: ...
  86. #
  87. def __iter__(self) -> Self: ...
  88. class ndindex:
  89. @overload
  90. def __init__(self, shape: tuple[SupportsIndex, ...], /) -> None: ...
  91. @overload
  92. def __init__(self, /, *shape: SupportsIndex) -> None: ...
  93. #
  94. def __iter__(self) -> Self: ...
  95. def __next__(self) -> _AnyShape: ...
  96. class nd_grid(Generic[_BoolT_co]):
  97. __slots__ = ("sparse",)
  98. sparse: _BoolT_co
  99. def __init__(self, sparse: _BoolT_co = ...) -> None: ... # stubdefaulter: ignore[missing-default]
  100. @overload
  101. def __getitem__(self: nd_grid[L[False]], key: slice | Sequence[slice]) -> NDArray[Incomplete]: ...
  102. @overload
  103. def __getitem__(self: nd_grid[L[True]], key: slice | Sequence[slice]) -> tuple[NDArray[Incomplete], ...]: ...
  104. @final
  105. class MGridClass(nd_grid[L[False]]):
  106. __slots__ = ()
  107. def __init__(self) -> None: ...
  108. @final
  109. class OGridClass(nd_grid[L[True]]):
  110. __slots__ = ()
  111. def __init__(self) -> None: ...
  112. class AxisConcatenator(Generic[_AxisT_co, _MatrixT_co, _NDMinT_co, _Trans1DT_co]):
  113. __slots__ = "axis", "matrix", "ndmin", "trans1d"
  114. makemat: ClassVar[type[np.matrix[tuple[int, int], np.dtype]]]
  115. axis: _AxisT_co
  116. matrix: _MatrixT_co
  117. ndmin: _NDMinT_co
  118. trans1d: _Trans1DT_co
  119. # NOTE: mypy does not understand that these default values are the same as the
  120. # TypeVar defaults. Since the workaround would require us to write 16 overloads,
  121. # we ignore the assignment type errors here.
  122. def __init__(
  123. self,
  124. /,
  125. axis: _AxisT_co = 0, # type: ignore[assignment]
  126. matrix: _MatrixT_co = False, # type: ignore[assignment]
  127. ndmin: _NDMinT_co = 1, # type: ignore[assignment]
  128. trans1d: _Trans1DT_co = -1, # type: ignore[assignment]
  129. ) -> None: ...
  130. # TODO(jorenham): annotate this
  131. def __getitem__(self, key: Incomplete, /) -> Incomplete: ...
  132. def __len__(self, /) -> L[0]: ...
  133. # Keep in sync with _core.multiarray.concatenate
  134. @staticmethod
  135. @overload
  136. def concatenate(
  137. arrays: _ArrayLike[_ScalarT],
  138. /,
  139. axis: SupportsIndex | None = 0,
  140. out: None = None,
  141. *,
  142. dtype: None = None,
  143. casting: _CastingKind | None = "same_kind",
  144. ) -> NDArray[_ScalarT]: ...
  145. @staticmethod
  146. @overload
  147. def concatenate(
  148. arrays: SupportsLenAndGetItem[ArrayLike],
  149. /,
  150. axis: SupportsIndex | None = 0,
  151. out: None = None,
  152. *,
  153. dtype: _DTypeLike[_ScalarT],
  154. casting: _CastingKind | None = "same_kind",
  155. ) -> NDArray[_ScalarT]: ...
  156. @staticmethod
  157. @overload
  158. def concatenate(
  159. arrays: SupportsLenAndGetItem[ArrayLike],
  160. /,
  161. axis: SupportsIndex | None = 0,
  162. out: None = None,
  163. *,
  164. dtype: DTypeLike | None = None,
  165. casting: _CastingKind | None = "same_kind",
  166. ) -> NDArray[Incomplete]: ...
  167. @staticmethod
  168. @overload
  169. def concatenate(
  170. arrays: SupportsLenAndGetItem[ArrayLike],
  171. /,
  172. axis: SupportsIndex | None = 0,
  173. *,
  174. out: _ArrayT,
  175. dtype: DTypeLike | None = None,
  176. casting: _CastingKind | None = "same_kind",
  177. ) -> _ArrayT: ...
  178. @staticmethod
  179. @overload
  180. def concatenate(
  181. arrays: SupportsLenAndGetItem[ArrayLike],
  182. /,
  183. axis: SupportsIndex | None,
  184. out: _ArrayT,
  185. *,
  186. dtype: DTypeLike | None = None,
  187. casting: _CastingKind | None = "same_kind",
  188. ) -> _ArrayT: ...
  189. @final
  190. class RClass(AxisConcatenator[L[0], L[False], L[1], L[-1]]):
  191. __slots__ = ()
  192. def __init__(self, /) -> None: ...
  193. @final
  194. class CClass(AxisConcatenator[L[-1], L[False], L[2], L[0]]):
  195. __slots__ = ()
  196. def __init__(self, /) -> None: ...
  197. class IndexExpression(Generic[_BoolT_co]):
  198. __slots__ = ("maketuple",)
  199. maketuple: _BoolT_co
  200. def __init__(self, maketuple: _BoolT_co) -> None: ...
  201. @overload
  202. def __getitem__(self, item: _TupleT) -> _TupleT: ...
  203. @overload
  204. def __getitem__(self: IndexExpression[L[True]], item: _T) -> tuple[_T]: ...
  205. @overload
  206. def __getitem__(self: IndexExpression[L[False]], item: _T) -> _T: ...
  207. @overload
  208. def ix_(*args: _FiniteNestedSequence[_HasDType[_DTypeT]]) -> tuple[np.ndarray[_AnyShape, _DTypeT], ...]: ...
  209. @overload
  210. def ix_(*args: str | _NestedSequence[str]) -> tuple[NDArray[np.str_], ...]: ...
  211. @overload
  212. def ix_(*args: bytes | _NestedSequence[bytes]) -> tuple[NDArray[np.bytes_], ...]: ...
  213. @overload
  214. def ix_(*args: bool | _NestedSequence[bool]) -> tuple[NDArray[np.bool], ...]: ...
  215. @overload
  216. def ix_(*args: int | _NestedSequence[int]) -> tuple[NDArray[np.intp], ...]: ...
  217. @overload
  218. def ix_(*args: float | _NestedSequence[float]) -> tuple[NDArray[np.float64], ...]: ...
  219. @overload
  220. def ix_(*args: complex | _NestedSequence[complex]) -> tuple[NDArray[np.complex128], ...]: ...
  221. #
  222. def fill_diagonal(a: NDArray[Any], val: object, wrap: bool = False) -> None: ...
  223. #
  224. def diag_indices(n: int, ndim: int = 2) -> tuple[NDArray[np.intp], ...]: ...
  225. def diag_indices_from(arr: ArrayLike) -> tuple[NDArray[np.intp], ...]: ...
  226. #
  227. mgrid: Final[MGridClass] = ...
  228. ogrid: Final[OGridClass] = ...
  229. r_: Final[RClass] = ...
  230. c_: Final[CClass] = ...
  231. index_exp: Final[IndexExpression[L[True]]] = ...
  232. s_: Final[IndexExpression[L[False]]] = ...