diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/__init__.pyi | 308 | ||||
-rw-r--r-- | numpy/core/multiarray.pyi | 4 | ||||
-rw-r--r-- | numpy/core/shape_base.pyi | 4 | ||||
-rw-r--r-- | numpy/lib/arraypad.pyi | 4 | ||||
-rw-r--r-- | numpy/lib/index_tricks.pyi | 18 | ||||
-rw-r--r-- | numpy/lib/shape_base.pyi | 4 | ||||
-rw-r--r-- | numpy/lib/stride_tricks.pyi | 4 | ||||
-rw-r--r-- | numpy/lib/twodim_base.pyi | 4 | ||||
-rw-r--r-- | numpy/lib/type_check.pyi | 4 | ||||
-rw-r--r-- | numpy/typing/__init__.py | 5 | ||||
-rw-r--r-- | numpy/typing/_array_like.py | 33 | ||||
-rw-r--r-- | numpy/typing/_nested_sequence.py | 93 | ||||
-rw-r--r-- | numpy/typing/tests/data/fail/comparisons.py | 4 | ||||
-rw-r--r-- | numpy/typing/tests/data/fail/multiarray.py | 2 | ||||
-rw-r--r-- | numpy/typing/tests/data/fail/nested_sequence.py | 17 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/nested_sequence.py | 23 |
16 files changed, 243 insertions, 288 deletions
diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index b34cdecc7..33e2af72c 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -20,7 +20,7 @@ from numpy.typing import ( NDArray, _SupportsArray, _NestedSequence, - _RecursiveSequence, + _FiniteNestedSequence, _SupportsArray, _ArrayLikeBool_co, _ArrayLikeUInt_co, @@ -1992,7 +1992,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): # strings, it will pass through the final overload otherwise @overload - def __lt__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __lt__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __lt__(self: _ArrayNumber_co, other: _ArrayLikeNumber_co) -> NDArray[bool_]: ... @overload @@ -2003,14 +2003,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __lt__(self: NDArray[object_], other: Any) -> NDArray[bool_]: ... @overload def __lt__(self: NDArray[Any], other: _ArrayLikeObject_co) -> NDArray[bool_]: ... - @overload - def __lt__( - self: NDArray[Union[number[Any], datetime64, timedelta64, bool_]], - other: _RecursiveSequence, - ) -> NDArray[bool_]: ... @overload - def __le__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __le__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __le__(self: _ArrayNumber_co, other: _ArrayLikeNumber_co) -> NDArray[bool_]: ... @overload @@ -2021,14 +2016,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __le__(self: NDArray[object_], other: Any) -> NDArray[bool_]: ... @overload def __le__(self: NDArray[Any], other: _ArrayLikeObject_co) -> NDArray[bool_]: ... - @overload - def __le__( - self: NDArray[Union[number[Any], datetime64, timedelta64, bool_]], - other: _RecursiveSequence, - ) -> NDArray[bool_]: ... @overload - def __gt__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __gt__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __gt__(self: _ArrayNumber_co, other: _ArrayLikeNumber_co) -> NDArray[bool_]: ... @overload @@ -2039,14 +2029,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __gt__(self: NDArray[object_], other: Any) -> NDArray[bool_]: ... @overload def __gt__(self: NDArray[Any], other: _ArrayLikeObject_co) -> NDArray[bool_]: ... - @overload - def __gt__( - self: NDArray[Union[number[Any], datetime64, timedelta64, bool_]], - other: _RecursiveSequence, - ) -> NDArray[bool_]: ... @overload - def __ge__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __ge__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ge__(self: _ArrayNumber_co, other: _ArrayLikeNumber_co) -> NDArray[bool_]: ... @overload @@ -2057,11 +2042,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __ge__(self: NDArray[object_], other: Any) -> NDArray[bool_]: ... @overload def __ge__(self: NDArray[Any], other: _ArrayLikeObject_co) -> NDArray[bool_]: ... - @overload - def __ge__( - self: NDArray[Union[number[Any], datetime64, timedelta64, bool_]], - other: _RecursiveSequence, - ) -> NDArray[bool_]: ... # Unary ops @overload @@ -2099,7 +2079,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): # Binary ops # NOTE: `ndarray` does not implement `__imatmul__` @overload - def __matmul__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __matmul__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __matmul__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2114,14 +2094,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __matmul__(self: NDArray[object_], other: Any) -> Any: ... @overload def __matmul__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __matmul__( - self: _ArrayNumber_co, - other: _RecursiveSequence, - ) -> Any: ... @overload - def __rmatmul__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __rmatmul__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rmatmul__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2136,14 +2111,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __rmatmul__(self: NDArray[object_], other: Any) -> Any: ... @overload def __rmatmul__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rmatmul__( - self: _ArrayNumber_co, - other: _RecursiveSequence, - ) -> Any: ... @overload - def __mod__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __mod__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __mod__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2153,19 +2123,14 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __mod__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> NDArray[floating[Any]]: ... # type: ignore[misc] @overload - def __mod__(self: _ArrayTD64_co, other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[timedelta64]: ... + def __mod__(self: _ArrayTD64_co, other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[timedelta64]: ... @overload def __mod__(self: NDArray[object_], other: Any) -> Any: ... @overload def __mod__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __mod__( - self: NDArray[Union[bool_, integer[Any], floating[Any], timedelta64]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __rmod__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __rmod__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rmod__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2175,19 +2140,14 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rmod__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> NDArray[floating[Any]]: ... # type: ignore[misc] @overload - def __rmod__(self: _ArrayTD64_co, other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[timedelta64]: ... + def __rmod__(self: _ArrayTD64_co, other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[timedelta64]: ... @overload def __rmod__(self: NDArray[object_], other: Any) -> Any: ... @overload def __rmod__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rmod__( - self: NDArray[Union[bool_, integer[Any], floating[Any], timedelta64]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __divmod__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __divmod__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __divmod__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> _2Tuple[NDArray[int8]]: ... # type: ignore[misc] @overload @@ -2197,15 +2157,10 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __divmod__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> _2Tuple[NDArray[floating[Any]]]: ... # type: ignore[misc] @overload - def __divmod__(self: _ArrayTD64_co, other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ... - @overload - def __divmod__( - self: NDArray[Union[bool_, integer[Any], floating[Any], timedelta64]], - other: _RecursiveSequence, - ) -> _2Tuple[Any]: ... + def __divmod__(self: _ArrayTD64_co, other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ... @overload - def __rdivmod__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __rdivmod__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rdivmod__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> _2Tuple[NDArray[int8]]: ... # type: ignore[misc] @overload @@ -2215,15 +2170,10 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rdivmod__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> _2Tuple[NDArray[floating[Any]]]: ... # type: ignore[misc] @overload - def __rdivmod__(self: _ArrayTD64_co, other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ... - @overload - def __rdivmod__( - self: NDArray[Union[bool_, integer[Any], floating[Any], timedelta64]], - other: _RecursiveSequence, - ) -> _2Tuple[Any]: ... + def __rdivmod__(self: _ArrayTD64_co, other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> Tuple[NDArray[int64], NDArray[timedelta64]]: ... @overload - def __add__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __add__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __add__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2244,14 +2194,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __add__(self: NDArray[object_], other: Any) -> Any: ... @overload def __add__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __add__( - self: NDArray[Union[bool_, number[Any], timedelta64, datetime64]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __radd__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __radd__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __radd__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2272,14 +2217,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __radd__(self: NDArray[object_], other: Any) -> Any: ... @overload def __radd__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __radd__( - self: NDArray[Union[bool_, number[Any], timedelta64, datetime64]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __sub__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __sub__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __sub__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NoReturn: ... @overload @@ -2300,14 +2240,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __sub__(self: NDArray[object_], other: Any) -> Any: ... @overload def __sub__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __sub__( - self: NDArray[Union[bool_, number[Any], timedelta64, datetime64]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __rsub__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __rsub__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rsub__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NoReturn: ... @overload @@ -2328,14 +2263,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __rsub__(self: NDArray[object_], other: Any) -> Any: ... @overload def __rsub__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rsub__( - self: NDArray[Union[bool_, number[Any], timedelta64, datetime64]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __mul__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __mul__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __mul__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2354,14 +2284,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __mul__(self: NDArray[object_], other: Any) -> Any: ... @overload def __mul__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __mul__( - self: NDArray[Union[bool_, number[Any], timedelta64]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __rmul__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __rmul__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rmul__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2380,14 +2305,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __rmul__(self: NDArray[object_], other: Any) -> Any: ... @overload def __rmul__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rmul__( - self: NDArray[Union[bool_, number[Any], timedelta64]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __floordiv__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __floordiv__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __floordiv__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2397,7 +2317,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __floordiv__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> NDArray[floating[Any]]: ... # type: ignore[misc] @overload - def __floordiv__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[int64]: ... + def __floordiv__(self: NDArray[timedelta64], other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[int64]: ... @overload def __floordiv__(self: NDArray[timedelta64], other: _ArrayLikeBool_co) -> NoReturn: ... @overload @@ -2406,14 +2326,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __floordiv__(self: NDArray[object_], other: Any) -> Any: ... @overload def __floordiv__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __floordiv__( - self: NDArray[Union[bool_, number[Any], timedelta64]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __rfloordiv__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __rfloordiv__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rfloordiv__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2423,7 +2338,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rfloordiv__(self: _ArrayFloat_co, other: _ArrayLikeFloat_co) -> NDArray[floating[Any]]: ... # type: ignore[misc] @overload - def __rfloordiv__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[int64]: ... + def __rfloordiv__(self: NDArray[timedelta64], other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[int64]: ... @overload def __rfloordiv__(self: NDArray[bool_], other: _ArrayLikeTD64_co) -> NoReturn: ... @overload @@ -2432,14 +2347,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __rfloordiv__(self: NDArray[object_], other: Any) -> Any: ... @overload def __rfloordiv__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rfloordiv__( - self: NDArray[Union[bool_, number[Any], timedelta64]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __pow__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __pow__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __pow__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2454,14 +2364,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __pow__(self: NDArray[object_], other: Any) -> Any: ... @overload def __pow__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __pow__( - self: NDArray[Union[bool_, number[Any]]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __rpow__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __rpow__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rpow__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2476,14 +2381,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __rpow__(self: NDArray[object_], other: Any) -> Any: ... @overload def __rpow__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rpow__( - self: NDArray[Union[bool_, number[Any]]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __truediv__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __truediv__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __truediv__(self: _ArrayInt_co, other: _ArrayInt_co) -> NDArray[float64]: ... # type: ignore[misc] @overload @@ -2491,7 +2391,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __truediv__(self: _ArrayComplex_co, other: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ... # type: ignore[misc] @overload - def __truediv__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[float64]: ... + def __truediv__(self: NDArray[timedelta64], other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[float64]: ... @overload def __truediv__(self: NDArray[timedelta64], other: _ArrayLikeBool_co) -> NoReturn: ... @overload @@ -2500,14 +2400,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __truediv__(self: NDArray[object_], other: Any) -> Any: ... @overload def __truediv__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __truediv__( - self: NDArray[Union[bool_, number[Any], timedelta64]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __rtruediv__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __rtruediv__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rtruediv__(self: _ArrayInt_co, other: _ArrayInt_co) -> NDArray[float64]: ... # type: ignore[misc] @overload @@ -2515,7 +2410,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __rtruediv__(self: _ArrayComplex_co, other: _ArrayLikeComplex_co) -> NDArray[complexfloating[Any, Any]]: ... # type: ignore[misc] @overload - def __rtruediv__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[float64]: ... + def __rtruediv__(self: NDArray[timedelta64], other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[float64]: ... @overload def __rtruediv__(self: NDArray[bool_], other: _ArrayLikeTD64_co) -> NoReturn: ... @overload @@ -2524,14 +2419,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __rtruediv__(self: NDArray[object_], other: Any) -> Any: ... @overload def __rtruediv__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rtruediv__( - self: NDArray[Union[bool_, number[Any], timedelta64]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __lshift__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __lshift__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __lshift__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2542,14 +2432,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __lshift__(self: NDArray[object_], other: Any) -> Any: ... @overload def __lshift__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __lshift__( - self: NDArray[Union[bool_, integer[Any]]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __rlshift__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __rlshift__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rlshift__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2560,14 +2445,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __rlshift__(self: NDArray[object_], other: Any) -> Any: ... @overload def __rlshift__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rlshift__( - self: NDArray[Union[bool_, integer[Any]]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __rshift__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __rshift__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rshift__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2578,14 +2458,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __rshift__(self: NDArray[object_], other: Any) -> Any: ... @overload def __rshift__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rshift__( - self: NDArray[Union[bool_, integer[Any]]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __rrshift__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __rrshift__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rrshift__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[int8]: ... # type: ignore[misc] @overload @@ -2596,14 +2471,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __rrshift__(self: NDArray[object_], other: Any) -> Any: ... @overload def __rrshift__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rrshift__( - self: NDArray[Union[bool_, integer[Any]]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __and__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __and__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __and__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2614,14 +2484,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __and__(self: NDArray[object_], other: Any) -> Any: ... @overload def __and__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __and__( - self: NDArray[Union[bool_, integer[Any]]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __rand__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __rand__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rand__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2632,14 +2497,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __rand__(self: NDArray[object_], other: Any) -> Any: ... @overload def __rand__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rand__( - self: NDArray[Union[bool_, integer[Any]]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __xor__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __xor__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __xor__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2650,14 +2510,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __xor__(self: NDArray[object_], other: Any) -> Any: ... @overload def __xor__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __xor__( - self: NDArray[Union[bool_, integer[Any]]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __rxor__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __rxor__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __rxor__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2668,14 +2523,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __rxor__(self: NDArray[object_], other: Any) -> Any: ... @overload def __rxor__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __rxor__( - self: NDArray[Union[bool_, integer[Any]]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __or__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __or__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __or__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2686,14 +2536,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __or__(self: NDArray[object_], other: Any) -> Any: ... @overload def __or__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __or__( - self: NDArray[Union[bool_, integer[Any]]], - other: _RecursiveSequence, - ) -> Any: ... @overload - def __ror__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __ror__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ror__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... # type: ignore[misc] @overload @@ -2704,15 +2549,10 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __ror__(self: NDArray[object_], other: Any) -> Any: ... @overload def __ror__(self: NDArray[Any], other: _ArrayLikeObject_co) -> Any: ... - @overload - def __ror__( - self: NDArray[Union[bool_, integer[Any]]], - other: _RecursiveSequence, - ) -> Any: ... # `np.generic` does not support inplace operations @overload # type: ignore[misc] - def __iadd__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __iadd__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __iadd__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... @overload @@ -2729,11 +2569,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __iadd__(self: NDArray[datetime64], other: _ArrayLikeTD64_co) -> NDArray[datetime64]: ... @overload def __iadd__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload - def __iadd__(self: NDArray[_ScalarType], other: _RecursiveSequence) -> NDArray[_ScalarType]: ... @overload # type: ignore[misc] - def __isub__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __isub__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __isub__(self: NDArray[unsignedinteger[_NBit1]], other: _ArrayLikeUInt_co) -> NDArray[unsignedinteger[_NBit1]]: ... @overload @@ -2748,11 +2586,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __isub__(self: NDArray[datetime64], other: _ArrayLikeTD64_co) -> NDArray[datetime64]: ... @overload def __isub__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload - def __isub__(self: NDArray[_ScalarType], other: _RecursiveSequence) -> NDArray[_ScalarType]: ... @overload # type: ignore[misc] - def __imul__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __imul__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __imul__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... @overload @@ -2767,11 +2603,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __imul__(self: NDArray[timedelta64], other: _ArrayLikeFloat_co) -> NDArray[timedelta64]: ... @overload def __imul__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload - def __imul__(self: NDArray[_ScalarType], other: _RecursiveSequence) -> NDArray[_ScalarType]: ... @overload # type: ignore[misc] - def __itruediv__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __itruediv__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __itruediv__(self: NDArray[floating[_NBit1]], other: _ArrayLikeFloat_co) -> NDArray[floating[_NBit1]]: ... @overload @@ -2782,11 +2616,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __itruediv__(self: NDArray[timedelta64], other: _ArrayLikeInt_co) -> NDArray[timedelta64]: ... @overload def __itruediv__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload - def __itruediv__(self: NDArray[_ScalarType], other: _RecursiveSequence) -> NDArray[_ScalarType]: ... @overload # type: ignore[misc] - def __ifloordiv__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __ifloordiv__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ifloordiv__(self: NDArray[unsignedinteger[_NBit1]], other: _ArrayLikeUInt_co) -> NDArray[unsignedinteger[_NBit1]]: ... @overload @@ -2801,11 +2633,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __ifloordiv__(self: NDArray[timedelta64], other: _ArrayLikeInt_co) -> NDArray[timedelta64]: ... @overload def __ifloordiv__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload - def __ifloordiv__(self: NDArray[_ScalarType], other: _RecursiveSequence) -> NDArray[_ScalarType]: ... @overload # type: ignore[misc] - def __ipow__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __ipow__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ipow__(self: NDArray[unsignedinteger[_NBit1]], other: _ArrayLikeUInt_co) -> NDArray[unsignedinteger[_NBit1]]: ... @overload @@ -2816,11 +2646,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __ipow__(self: NDArray[complexfloating[_NBit1, _NBit1]], other: _ArrayLikeComplex_co) -> NDArray[complexfloating[_NBit1, _NBit1]]: ... @overload def __ipow__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload - def __ipow__(self: NDArray[_ScalarType], other: _RecursiveSequence) -> NDArray[_ScalarType]: ... @overload # type: ignore[misc] - def __imod__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __imod__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __imod__(self: NDArray[unsignedinteger[_NBit1]], other: _ArrayLikeUInt_co) -> NDArray[unsignedinteger[_NBit1]]: ... @overload @@ -2828,36 +2656,30 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): @overload def __imod__(self: NDArray[floating[_NBit1]], other: _ArrayLikeFloat_co) -> NDArray[floating[_NBit1]]: ... @overload - def __imod__(self: NDArray[timedelta64], other: _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[timedelta64]: ... + def __imod__(self: NDArray[timedelta64], other: _SupportsArray[dtype[timedelta64]] | _NestedSequence[_SupportsArray[dtype[timedelta64]]]) -> NDArray[timedelta64]: ... @overload def __imod__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload - def __imod__(self: NDArray[_ScalarType], other: _RecursiveSequence) -> NDArray[_ScalarType]: ... @overload # type: ignore[misc] - def __ilshift__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __ilshift__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ilshift__(self: NDArray[unsignedinteger[_NBit1]], other: _ArrayLikeUInt_co) -> NDArray[unsignedinteger[_NBit1]]: ... @overload def __ilshift__(self: NDArray[signedinteger[_NBit1]], other: _ArrayLikeInt_co) -> NDArray[signedinteger[_NBit1]]: ... @overload def __ilshift__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload - def __ilshift__(self: NDArray[_ScalarType], other: _RecursiveSequence) -> NDArray[_ScalarType]: ... @overload # type: ignore[misc] - def __irshift__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __irshift__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __irshift__(self: NDArray[unsignedinteger[_NBit1]], other: _ArrayLikeUInt_co) -> NDArray[unsignedinteger[_NBit1]]: ... @overload def __irshift__(self: NDArray[signedinteger[_NBit1]], other: _ArrayLikeInt_co) -> NDArray[signedinteger[_NBit1]]: ... @overload def __irshift__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload - def __irshift__(self: NDArray[_ScalarType], other: _RecursiveSequence) -> NDArray[_ScalarType]: ... @overload # type: ignore[misc] - def __iand__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __iand__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __iand__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... @overload @@ -2866,11 +2688,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __iand__(self: NDArray[signedinteger[_NBit1]], other: _ArrayLikeInt_co) -> NDArray[signedinteger[_NBit1]]: ... @overload def __iand__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload - def __iand__(self: NDArray[_ScalarType], other: _RecursiveSequence) -> NDArray[_ScalarType]: ... @overload # type: ignore[misc] - def __ixor__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __ixor__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ixor__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... @overload @@ -2879,11 +2699,9 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __ixor__(self: NDArray[signedinteger[_NBit1]], other: _ArrayLikeInt_co) -> NDArray[signedinteger[_NBit1]]: ... @overload def __ixor__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload - def __ixor__(self: NDArray[_ScalarType], other: _RecursiveSequence) -> NDArray[_ScalarType]: ... @overload # type: ignore[misc] - def __ior__(self: NDArray[Any], other: _NestedSequence[Union[str, bytes]]) -> NoReturn: ... + def __ior__(self: NDArray[Any], other: bytes | _NestedSequence[bytes]) -> NoReturn: ... @overload def __ior__(self: NDArray[bool_], other: _ArrayLikeBool_co) -> NDArray[bool_]: ... @overload @@ -2892,8 +2710,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]): def __ior__(self: NDArray[signedinteger[_NBit1]], other: _ArrayLikeInt_co) -> NDArray[signedinteger[_NBit1]]: ... @overload def __ior__(self: NDArray[object_], other: Any) -> NDArray[object_]: ... - @overload - def __ior__(self: NDArray[_ScalarType], other: _RecursiveSequence) -> NDArray[_ScalarType]: ... # Keep `dtype` at the bottom to avoid name conflicts with `np.dtype` @property @@ -3734,22 +3550,20 @@ class ndenumerate(Generic[_ScalarType]): iter: flatiter[NDArray[_ScalarType]] @overload def __new__( - cls, arr: _NestedSequence[_SupportsArray[dtype[_ScalarType]]], + cls, arr: _FiniteNestedSequence[_SupportsArray[dtype[_ScalarType]]], ) -> ndenumerate[_ScalarType]: ... @overload - def __new__(cls, arr: _NestedSequence[str]) -> ndenumerate[str_]: ... - @overload - def __new__(cls, arr: _NestedSequence[bytes]) -> ndenumerate[bytes_]: ... + def __new__(cls, arr: str | _NestedSequence[str]) -> ndenumerate[str_]: ... @overload - def __new__(cls, arr: _NestedSequence[bool]) -> ndenumerate[bool_]: ... + def __new__(cls, arr: bytes | _NestedSequence[bytes]) -> ndenumerate[bytes_]: ... @overload - def __new__(cls, arr: _NestedSequence[int]) -> ndenumerate[int_]: ... + def __new__(cls, arr: bool | _NestedSequence[bool]) -> ndenumerate[bool_]: ... @overload - def __new__(cls, arr: _NestedSequence[float]) -> ndenumerate[float_]: ... + def __new__(cls, arr: int | _NestedSequence[int]) -> ndenumerate[int_]: ... @overload - def __new__(cls, arr: _NestedSequence[complex]) -> ndenumerate[complex_]: ... + def __new__(cls, arr: float | _NestedSequence[float]) -> ndenumerate[float_]: ... @overload - def __new__(cls, arr: _RecursiveSequence) -> ndenumerate[Any]: ... + def __new__(cls, arr: complex | _NestedSequence[complex]) -> ndenumerate[complex_]: ... def __next__(self: ndenumerate[_ScalarType]) -> Tuple[_Shape, _ScalarType]: ... def __iter__(self: _T) -> _T: ... diff --git a/numpy/core/multiarray.pyi b/numpy/core/multiarray.pyi index cad6047c9..6cc3f0d43 100644 --- a/numpy/core/multiarray.pyi +++ b/numpy/core/multiarray.pyi @@ -65,7 +65,7 @@ from numpy.typing import ( NDArray, ArrayLike, _SupportsArray, - _NestedSequence, + _FiniteNestedSequence, _ArrayLikeBool_co, _ArrayLikeUInt_co, _ArrayLikeInt_co, @@ -91,7 +91,7 @@ _DTypeLike = Union[ Type[_SCT], _SupportsDType[dtype[_SCT]], ] -_ArrayLike = _NestedSequence[_SupportsArray[dtype[_SCT]]] +_ArrayLike = _FiniteNestedSequence[_SupportsArray[dtype[_SCT]]] # Valid time units _UnitKind = L[ diff --git a/numpy/core/shape_base.pyi b/numpy/core/shape_base.pyi index d7914697d..159ad2781 100644 --- a/numpy/core/shape_base.pyi +++ b/numpy/core/shape_base.pyi @@ -1,12 +1,12 @@ from typing import TypeVar, overload, List, Sequence, Any, SupportsIndex from numpy import generic, dtype -from numpy.typing import ArrayLike, NDArray, _NestedSequence, _SupportsArray +from numpy.typing import ArrayLike, NDArray, _FiniteNestedSequence, _SupportsArray _SCT = TypeVar("_SCT", bound=generic) _ArrayType = TypeVar("_ArrayType", bound=NDArray[Any]) -_ArrayLike = _NestedSequence[_SupportsArray[dtype[_SCT]]] +_ArrayLike = _FiniteNestedSequence[_SupportsArray[dtype[_SCT]]] __all__: List[str] diff --git a/numpy/lib/arraypad.pyi b/numpy/lib/arraypad.pyi index 49ce8e683..d7c5f4844 100644 --- a/numpy/lib/arraypad.pyi +++ b/numpy/lib/arraypad.pyi @@ -15,7 +15,7 @@ from numpy.typing import ( ArrayLike, NDArray, _ArrayLikeInt, - _NestedSequence, + _FiniteNestedSequence, _SupportsArray, ) @@ -45,7 +45,7 @@ _ModeKind = L[ "empty", ] -_ArrayLike = _NestedSequence[_SupportsArray[dtype[_SCT]]] +_ArrayLike = _FiniteNestedSequence[_SupportsArray[dtype[_SCT]]] __all__: List[str] diff --git a/numpy/lib/index_tricks.pyi b/numpy/lib/index_tricks.pyi index 530be3cae..d16faf81a 100644 --- a/numpy/lib/index_tricks.pyi +++ b/numpy/lib/index_tricks.pyi @@ -33,7 +33,7 @@ from numpy.typing import ( # Arrays ArrayLike, _NestedSequence, - _RecursiveSequence, + _FiniteNestedSequence, NDArray, _ArrayLikeInt, @@ -59,21 +59,19 @@ _ArrayType = TypeVar("_ArrayType", bound=ndarray[Any, Any]) __all__: List[str] @overload -def ix_(*args: _NestedSequence[_SupportsDType[_DType]]) -> Tuple[ndarray[Any, _DType], ...]: ... +def ix_(*args: _FiniteNestedSequence[_SupportsDType[_DType]]) -> Tuple[ndarray[Any, _DType], ...]: ... @overload -def ix_(*args: _NestedSequence[str]) -> Tuple[NDArray[str_], ...]: ... +def ix_(*args: str | _NestedSequence[str]) -> Tuple[NDArray[str_], ...]: ... @overload -def ix_(*args: _NestedSequence[bytes]) -> Tuple[NDArray[bytes_], ...]: ... +def ix_(*args: bytes | _NestedSequence[bytes]) -> Tuple[NDArray[bytes_], ...]: ... @overload -def ix_(*args: _NestedSequence[bool]) -> Tuple[NDArray[bool_], ...]: ... +def ix_(*args: bool | _NestedSequence[bool]) -> Tuple[NDArray[bool_], ...]: ... @overload -def ix_(*args: _NestedSequence[int]) -> Tuple[NDArray[int_], ...]: ... +def ix_(*args: int | _NestedSequence[int]) -> Tuple[NDArray[int_], ...]: ... @overload -def ix_(*args: _NestedSequence[float]) -> Tuple[NDArray[float_], ...]: ... +def ix_(*args: float | _NestedSequence[float]) -> Tuple[NDArray[float_], ...]: ... @overload -def ix_(*args: _NestedSequence[complex]) -> Tuple[NDArray[complex_], ...]: ... -@overload -def ix_(*args: _RecursiveSequence) -> Tuple[NDArray[Any], ...]: ... +def ix_(*args: complex | _NestedSequence[complex]) -> Tuple[NDArray[complex_], ...]: ... class nd_grid(Generic[_BoolType]): sparse: _BoolType diff --git a/numpy/lib/shape_base.pyi b/numpy/lib/shape_base.pyi index 1598dc36c..8aa283d02 100644 --- a/numpy/lib/shape_base.pyi +++ b/numpy/lib/shape_base.pyi @@ -17,7 +17,7 @@ from numpy.typing import ( ArrayLike, NDArray, _ShapeLike, - _NestedSequence, + _FiniteNestedSequence, _SupportsDType, _ArrayLikeBool_co, _ArrayLikeUInt_co, @@ -31,7 +31,7 @@ from numpy.core.shape_base import vstack _SCT = TypeVar("_SCT", bound=generic) -_ArrayLike = _NestedSequence[_SupportsDType[dtype[_SCT]]] +_ArrayLike = _FiniteNestedSequence[_SupportsDType[dtype[_SCT]]] # The signatures of `__array_wrap__` and `__array_prepare__` are the same; # give them unique names for the sake of clarity diff --git a/numpy/lib/stride_tricks.pyi b/numpy/lib/stride_tricks.pyi index bafc46e9c..aad404107 100644 --- a/numpy/lib/stride_tricks.pyi +++ b/numpy/lib/stride_tricks.pyi @@ -6,12 +6,12 @@ from numpy.typing import ( ArrayLike, _ShapeLike, _Shape, - _NestedSequence, + _FiniteNestedSequence, _SupportsArray, ) _SCT = TypeVar("_SCT", bound=generic) -_ArrayLike = _NestedSequence[_SupportsArray[dtype[_SCT]]] +_ArrayLike = _FiniteNestedSequence[_SupportsArray[dtype[_SCT]]] __all__: List[str] diff --git a/numpy/lib/twodim_base.pyi b/numpy/lib/twodim_base.pyi index 007338d77..cba503ca3 100644 --- a/numpy/lib/twodim_base.pyi +++ b/numpy/lib/twodim_base.pyi @@ -33,7 +33,7 @@ from numpy.typing import ( _SupportsDType, ArrayLike, NDArray, - _NestedSequence, + _FiniteNestedSequence, _SupportsArray, _ArrayLikeInt_co, _ArrayLikeFloat_co, @@ -55,7 +55,7 @@ _DTypeLike = Union[ dtype[_SCT], _SupportsDType[dtype[_SCT]], ] -_ArrayLike = _NestedSequence[_SupportsArray[dtype[_SCT]]] +_ArrayLike = _FiniteNestedSequence[_SupportsArray[dtype[_SCT]]] __all__: List[str] diff --git a/numpy/lib/type_check.pyi b/numpy/lib/type_check.pyi index 5eb0e62d2..0a55dbf21 100644 --- a/numpy/lib/type_check.pyi +++ b/numpy/lib/type_check.pyi @@ -28,7 +28,7 @@ from numpy.typing import ( _64Bit, _SupportsDType, _ScalarLike_co, - _NestedSequence, + _FiniteNestedSequence, _SupportsArray, _DTypeLikeComplex, ) @@ -39,7 +39,7 @@ _SCT = TypeVar("_SCT", bound=generic) _NBit1 = TypeVar("_NBit1", bound=NBitBase) _NBit2 = TypeVar("_NBit2", bound=NBitBase) -_ArrayLike = _NestedSequence[_SupportsArray[dtype[_SCT]]] +_ArrayLike = _FiniteNestedSequence[_SupportsArray[dtype[_SCT]]] class _SupportsReal(Protocol[_T_co]): @property diff --git a/numpy/typing/__init__.py b/numpy/typing/__init__.py index 86d55e9ce..2bea3be86 100644 --- a/numpy/typing/__init__.py +++ b/numpy/typing/__init__.py @@ -219,6 +219,8 @@ class _32Bit(_64Bit): ... # type: ignore[misc] class _16Bit(_32Bit): ... # type: ignore[misc] class _8Bit(_16Bit): ... # type: ignore[misc] + +from ._nested_sequence import _NestedSequence from ._nbit import ( _NBitByte, _NBitShort, @@ -305,8 +307,7 @@ from ._dtype_like import ( from ._array_like import ( ArrayLike as ArrayLike, _ArrayLike, - _NestedSequence, - _RecursiveSequence, + _FiniteNestedSequence, _SupportsArray, _ArrayLikeInt, _ArrayLikeBool_co, diff --git a/numpy/typing/_array_like.py b/numpy/typing/_array_like.py index 6ea0eb662..02e5ee573 100644 --- a/numpy/typing/_array_like.py +++ b/numpy/typing/_array_like.py @@ -18,6 +18,7 @@ from numpy import ( str_, bytes_, ) +from ._nested_sequence import _NestedSequence _T = TypeVar("_T") _ScalarType = TypeVar("_ScalarType", bound=generic) @@ -32,21 +33,23 @@ _DType_co = TypeVar("_DType_co", covariant=True, bound="dtype[Any]") class _SupportsArray(Protocol[_DType_co]): def __array__(self) -> ndarray[Any, _DType_co]: ... -# TODO: Wait for support for recursive types -_NestedSequence = Union[ + +# TODO: Wait until mypy supports recursive objects in combination with typevars +_FiniteNestedSequence = Union[ _T, Sequence[_T], Sequence[Sequence[_T]], Sequence[Sequence[Sequence[_T]]], Sequence[Sequence[Sequence[Sequence[_T]]]], ] -_RecursiveSequence = Sequence[Sequence[Sequence[Sequence[Sequence[Any]]]]] # A union representing array-like objects; consists of two typevars: # One representing types that can be parametrized w.r.t. `np.dtype` # and another one for the rest _ArrayLike = Union[ + _SupportsArray[_DType], _NestedSequence[_SupportsArray[_DType]], + _T, _NestedSequence[_T], ] @@ -57,12 +60,9 @@ _ArrayLike = Union[ # is resolved. See also the mypy issue: # # https://github.com/python/typing/issues/593 -ArrayLike = Union[ - _RecursiveSequence, - _ArrayLike[ - dtype, - Union[bool, int, float, complex, str, bytes] - ], +ArrayLike = _ArrayLike[ + dtype, + Union[bool, int, float, complex, str, bytes], ] # `ArrayLike<X>_co`: array-like objects that can be coerced into `X` @@ -95,10 +95,19 @@ _ArrayLikeTD64_co = _ArrayLike[ "dtype[Union[bool_, integer[Any], timedelta64]]", Union[bool, int], ] -_ArrayLikeDT64_co = _NestedSequence[_SupportsArray["dtype[datetime64]"]] -_ArrayLikeObject_co = _NestedSequence[_SupportsArray["dtype[object_]"]] +_ArrayLikeDT64_co = Union[ + _SupportsArray["dtype[datetime64]"], + _NestedSequence[_SupportsArray["dtype[datetime64]"]], +] +_ArrayLikeObject_co = Union[ + _SupportsArray["dtype[object_]"], + _NestedSequence[_SupportsArray["dtype[object_]"]], +] -_ArrayLikeVoid_co = _NestedSequence[_SupportsArray["dtype[void]"]] +_ArrayLikeVoid_co = Union[ + _SupportsArray["dtype[void]"], + _NestedSequence[_SupportsArray["dtype[void]"]], +] _ArrayLikeStr_co = _ArrayLike[ "dtype[str_]", str, diff --git a/numpy/typing/_nested_sequence.py b/numpy/typing/_nested_sequence.py new file mode 100644 index 000000000..e3b8fc33f --- /dev/null +++ b/numpy/typing/_nested_sequence.py @@ -0,0 +1,93 @@ +"""A module containing the `_NestedSequence` protocol.""" + +from __future__ import annotations + +import sys +from typing import ( + Any, + Iterator, + overload, + TypeVar, + Protocol, +) + +__all__ = ["_NestedSequence"] + +_T_co = TypeVar("_T_co", covariant=True) + + +class _NestedSequence(Protocol[_T_co]): + """A protocol for representing nested sequences. + + Warning + ------- + `_NestedSequence` currently does not work in combination with typevars, + *e.g.* ``def func(a: _NestedSequnce[T]) -> T: ...``. + + See Also + -------- + `collections.abc.Sequence` + ABCs for read-only and mutable :term:`sequences`. + + Examples + -------- + .. code-block:: python + + >>> from __future__ import annotations + + >>> from typing import TYPE_CHECKING + >>> import numpy as np + >>> from numpy.typing import _NestedSequnce + + >>> def get_dtype(seq: _NestedSequnce[float]) -> np.dtype[np.float64]: + ... return np.asarray(seq).dtype + + >>> a = get_dtype([1.0]) + >>> b = get_dtype([[1.0]]) + >>> c = get_dtype([[[1.0]]]) + >>> d = get_dtype([[[[1.0]]]]) + + >>> if TYPE_CHECKING: + ... reveal_locals() + ... # note: Revealed local types are: + ... # note: a: numpy.dtype[numpy.floating[numpy.typing._64Bit]] + ... # note: b: numpy.dtype[numpy.floating[numpy.typing._64Bit]] + ... # note: c: numpy.dtype[numpy.floating[numpy.typing._64Bit]] + ... # note: d: numpy.dtype[numpy.floating[numpy.typing._64Bit]] + + """ + + def __len__(self, /) -> int: + """Implement ``len(self)``.""" + raise NotImplementedError + + @overload + def __getitem__(self, index: int, /) -> _T_co | _NestedSequence[_T_co]: ... + @overload + def __getitem__(self, index: slice, /) -> _NestedSequence[_T_co]: ... + + def __getitem__(self, index, /): + """Implement ``self[x]``.""" + raise NotImplementedError + + def __contains__(self, x: object, /) -> bool: + """Implement ``x in self``.""" + raise NotImplementedError + + def __iter__(self, /) -> Iterator[_T_co | _NestedSequence[_T_co]]: + """Implement ``iter(self)``.""" + raise NotImplementedError + + def __reversed__(self, /) -> Iterator[_T_co | _NestedSequence[_T_co]]: + """Implement ``reversed(self)``.""" + raise NotImplementedError + + def count(self, value: Any, /) -> int: + """Return the number of occurrences of `value`.""" + raise NotImplementedError + + def index( + self, value: Any, start: int = 0, stop: int = sys.maxsize, / + ) -> int: + """Return the first index of `value`.""" + raise NotImplementedError diff --git a/numpy/typing/tests/data/fail/comparisons.py b/numpy/typing/tests/data/fail/comparisons.py index cad1c6555..0432177e2 100644 --- a/numpy/typing/tests/data/fail/comparisons.py +++ b/numpy/typing/tests/data/fail/comparisons.py @@ -22,7 +22,7 @@ AR_M > AR_f # E: Unsupported operand types AR_M > AR_m # E: Unsupported operand types # Unfortunately `NoReturn` errors are not the most descriptive -_1 = AR_i > str() # E: Need type annotation +_1 = AR_i > str() # E: No overload variant _2 = AR_i > bytes() # E: Need type annotation -_3 = str() > AR_M # E: Need type annotation +_3 = str() > AR_M # E: Unsupported operand types _4 = bytes() > AR_M # E: Need type annotation diff --git a/numpy/typing/tests/data/fail/multiarray.py b/numpy/typing/tests/data/fail/multiarray.py index 50361ec43..603a0452b 100644 --- a/numpy/typing/tests/data/fail/multiarray.py +++ b/numpy/typing/tests/data/fail/multiarray.py @@ -42,7 +42,7 @@ np.datetime_data(int) # E: incompatible type np.busday_offset("2012", 10) # E: incompatible type -np.datetime_as_string("2012") # E: incompatible type +np.datetime_as_string("2012") # E: No overload variant np.compare_chararrays("a", b"a", "==", False) # E: No overload variant diff --git a/numpy/typing/tests/data/fail/nested_sequence.py b/numpy/typing/tests/data/fail/nested_sequence.py new file mode 100644 index 000000000..e28661a05 --- /dev/null +++ b/numpy/typing/tests/data/fail/nested_sequence.py @@ -0,0 +1,17 @@ +from typing import Sequence, Tuple, List +import numpy.typing as npt + +a: Sequence[float] +b: List[complex] +c: Tuple[str, ...] +d: int +e: str + +def func(a: npt._NestedSequence[int]) -> None: + ... + +reveal_type(func(a)) # E: incompatible type +reveal_type(func(b)) # E: incompatible type +reveal_type(func(c)) # E: incompatible type +reveal_type(func(d)) # E: incompatible type +reveal_type(func(e)) # E: incompatible type diff --git a/numpy/typing/tests/data/reveal/nested_sequence.py b/numpy/typing/tests/data/reveal/nested_sequence.py new file mode 100644 index 000000000..07e24e357 --- /dev/null +++ b/numpy/typing/tests/data/reveal/nested_sequence.py @@ -0,0 +1,23 @@ +from typing import Sequence, Tuple, List, Any +import numpy.typing as npt + +a: Sequence[int] +b: Sequence[Sequence[int]] +c: Sequence[Sequence[Sequence[int]]] +d: Sequence[Sequence[Sequence[Sequence[int]]]] +e: Sequence[bool] +f: Tuple[int, ...] +g: List[int] +h: Sequence[Any] + +def func(a: npt._NestedSequence[int]) -> None: + ... + +reveal_type(func(a)) # E: None +reveal_type(func(b)) # E: None +reveal_type(func(c)) # E: None +reveal_type(func(d)) # E: None +reveal_type(func(e)) # E: None +reveal_type(func(f)) # E: None +reveal_type(func(g)) # E: None +reveal_type(func(h)) # E: None |