_index_tricks_impl.pyi 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. from collections.abc import Sequence
  2. from typing import Any, ClassVar, Final, Generic, SupportsIndex, final, overload
  3. from typing import Literal as L
  4. from _typeshed import Incomplete
  5. from typing_extensions import Self, TypeVar, deprecated
  6. import numpy as np
  7. from numpy._core.multiarray import ravel_multi_index, unravel_index
  8. from numpy._typing import (
  9. ArrayLike,
  10. NDArray,
  11. _FiniteNestedSequence,
  12. _NestedSequence,
  13. _Shape,
  14. _SupportsArray,
  15. _SupportsDType,
  16. )
  17. __all__ = [ # noqa: RUF022
  18. "ravel_multi_index",
  19. "unravel_index",
  20. "mgrid",
  21. "ogrid",
  22. "r_",
  23. "c_",
  24. "s_",
  25. "index_exp",
  26. "ix_",
  27. "ndenumerate",
  28. "ndindex",
  29. "fill_diagonal",
  30. "diag_indices",
  31. "diag_indices_from",
  32. ]
  33. ###
  34. _T = TypeVar("_T")
  35. _TupleT = TypeVar("_TupleT", bound=tuple[Any, ...])
  36. _ArrayT = TypeVar("_ArrayT", bound=NDArray[Any])
  37. _DTypeT = TypeVar("_DTypeT", bound=np.dtype[Any])
  38. _ScalarT = TypeVar("_ScalarT", bound=np.generic)
  39. _ScalarT_co = TypeVar("_ScalarT_co", bound=np.generic, covariant=True)
  40. _BoolT_co = TypeVar("_BoolT_co", bound=bool, default=bool, covariant=True)
  41. _AxisT_co = TypeVar("_AxisT_co", bound=int, default=L[0], covariant=True)
  42. _MatrixT_co = TypeVar("_MatrixT_co", bound=bool, default=L[False], covariant=True)
  43. _NDMinT_co = TypeVar("_NDMinT_co", bound=int, default=L[1], covariant=True)
  44. _Trans1DT_co = TypeVar("_Trans1DT_co", bound=int, default=L[-1], covariant=True)
  45. ###
  46. class ndenumerate(Generic[_ScalarT_co]):
  47. @overload
  48. def __new__(cls, arr: _FiniteNestedSequence[_SupportsArray[np.dtype[_ScalarT]]]) -> ndenumerate[_ScalarT]: ...
  49. @overload
  50. def __new__(cls, arr: str | _NestedSequence[str]) -> ndenumerate[np.str_]: ...
  51. @overload
  52. def __new__(cls, arr: bytes | _NestedSequence[bytes]) -> ndenumerate[np.bytes_]: ...
  53. @overload
  54. def __new__(cls, arr: bool | _NestedSequence[bool]) -> ndenumerate[np.bool]: ...
  55. @overload
  56. def __new__(cls, arr: int | _NestedSequence[int]) -> ndenumerate[np.intp]: ...
  57. @overload
  58. def __new__(cls, arr: float | _NestedSequence[float]) -> ndenumerate[np.float64]: ...
  59. @overload
  60. def __new__(cls, arr: complex | _NestedSequence[complex]) -> ndenumerate[np.complex128]: ...
  61. @overload
  62. def __new__(cls, arr: object) -> ndenumerate[Any]: ...
  63. # The first overload is a (semi-)workaround for a mypy bug (tested with v1.10 and v1.11)
  64. @overload
  65. def __next__(
  66. self: ndenumerate[np.bool | np.number | np.flexible | np.datetime64 | np.timedelta64],
  67. /,
  68. ) -> tuple[tuple[int, ...], _ScalarT_co]: ...
  69. @overload
  70. def __next__(self: ndenumerate[np.object_], /) -> tuple[tuple[int, ...], Any]: ...
  71. @overload
  72. def __next__(self, /) -> tuple[tuple[int, ...], _ScalarT_co]: ...
  73. #
  74. def __iter__(self) -> Self: ...
  75. class ndindex:
  76. @overload
  77. def __init__(self, shape: tuple[SupportsIndex, ...], /) -> None: ...
  78. @overload
  79. def __init__(self, /, *shape: SupportsIndex) -> None: ...
  80. #
  81. def __iter__(self) -> Self: ...
  82. def __next__(self) -> tuple[int, ...]: ...
  83. #
  84. @deprecated("Deprecated since 1.20.0.")
  85. def ndincr(self, /) -> None: ...
  86. class nd_grid(Generic[_BoolT_co]):
  87. sparse: _BoolT_co
  88. def __init__(self, sparse: _BoolT_co = ...) -> None: ...
  89. @overload
  90. def __getitem__(self: nd_grid[L[False]], key: slice | Sequence[slice]) -> NDArray[Any]: ...
  91. @overload
  92. def __getitem__(self: nd_grid[L[True]], key: slice | Sequence[slice]) -> tuple[NDArray[Any], ...]: ...
  93. @final
  94. class MGridClass(nd_grid[L[False]]):
  95. def __init__(self) -> None: ...
  96. @final
  97. class OGridClass(nd_grid[L[True]]):
  98. def __init__(self) -> None: ...
  99. class AxisConcatenator(Generic[_AxisT_co, _MatrixT_co, _NDMinT_co, _Trans1DT_co]):
  100. __slots__ = "axis", "matrix", "ndmin", "trans1d"
  101. makemat: ClassVar[type[np.matrix[tuple[int, int], np.dtype[Any]]]]
  102. axis: _AxisT_co
  103. matrix: _MatrixT_co
  104. ndmin: _NDMinT_co
  105. trans1d: _Trans1DT_co
  106. #
  107. def __init__(
  108. self,
  109. /,
  110. axis: _AxisT_co = ...,
  111. matrix: _MatrixT_co = ...,
  112. ndmin: _NDMinT_co = ...,
  113. trans1d: _Trans1DT_co = ...,
  114. ) -> None: ...
  115. # TODO(jorenham): annotate this
  116. def __getitem__(self, key: Incomplete, /) -> Incomplete: ...
  117. def __len__(self, /) -> L[0]: ...
  118. #
  119. @staticmethod
  120. @overload
  121. def concatenate(*a: ArrayLike, axis: SupportsIndex | None = 0, out: _ArrayT) -> _ArrayT: ...
  122. @staticmethod
  123. @overload
  124. def concatenate(*a: ArrayLike, axis: SupportsIndex | None = 0, out: None = None) -> NDArray[Any]: ...
  125. @final
  126. class RClass(AxisConcatenator[L[0], L[False], L[1], L[-1]]):
  127. def __init__(self, /) -> None: ...
  128. @final
  129. class CClass(AxisConcatenator[L[-1], L[False], L[2], L[0]]):
  130. def __init__(self, /) -> None: ...
  131. class IndexExpression(Generic[_BoolT_co]):
  132. maketuple: _BoolT_co
  133. def __init__(self, maketuple: _BoolT_co) -> None: ...
  134. @overload
  135. def __getitem__(self, item: _TupleT) -> _TupleT: ...
  136. @overload
  137. def __getitem__(self: IndexExpression[L[True]], item: _T) -> tuple[_T]: ...
  138. @overload
  139. def __getitem__(self: IndexExpression[L[False]], item: _T) -> _T: ...
  140. @overload
  141. def ix_(*args: _FiniteNestedSequence[_SupportsDType[_DTypeT]]) -> tuple[np.ndarray[_Shape, _DTypeT], ...]: ...
  142. @overload
  143. def ix_(*args: str | _NestedSequence[str]) -> tuple[NDArray[np.str_], ...]: ...
  144. @overload
  145. def ix_(*args: bytes | _NestedSequence[bytes]) -> tuple[NDArray[np.bytes_], ...]: ...
  146. @overload
  147. def ix_(*args: bool | _NestedSequence[bool]) -> tuple[NDArray[np.bool], ...]: ...
  148. @overload
  149. def ix_(*args: int | _NestedSequence[int]) -> tuple[NDArray[np.intp], ...]: ...
  150. @overload
  151. def ix_(*args: float | _NestedSequence[float]) -> tuple[NDArray[np.float64], ...]: ...
  152. @overload
  153. def ix_(*args: complex | _NestedSequence[complex]) -> tuple[NDArray[np.complex128], ...]: ...
  154. #
  155. def fill_diagonal(a: NDArray[Any], val: object, wrap: bool = ...) -> None: ...
  156. #
  157. def diag_indices(n: int, ndim: int = ...) -> tuple[NDArray[np.intp], ...]: ...
  158. def diag_indices_from(arr: ArrayLike) -> tuple[NDArray[np.intp], ...]: ...
  159. #
  160. mgrid: Final[MGridClass] = ...
  161. ogrid: Final[OGridClass] = ...
  162. r_: Final[RClass] = ...
  163. c_: Final[CClass] = ...
  164. index_exp: Final[IndexExpression[L[True]]] = ...
  165. s_: Final[IndexExpression[L[False]]] = ...