Skip to content

TYP: Workaround for a mypy issue in ndarray.__iter__ #29218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ _RealNumberT = TypeVar("_RealNumberT", bound=floating | integer)
_FloatingT_co = TypeVar("_FloatingT_co", bound=floating, default=floating, covariant=True)
_IntegerT = TypeVar("_IntegerT", bound=integer)
_IntegerT_co = TypeVar("_IntegerT_co", bound=integer, default=integer, covariant=True)
_NonObjectScalarT = TypeVar("_NonObjectScalarT", bound=np.bool | number | flexible | datetime64 | timedelta64)

_NBit = TypeVar("_NBit", bound=NBitBase, default=Any) # pyright: ignore[reportDeprecated]
_NBit1 = TypeVar("_NBit1", bound=NBitBase, default=Any) # pyright: ignore[reportDeprecated]
Expand Down Expand Up @@ -2572,10 +2573,13 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
def __len__(self) -> int: ...
def __contains__(self, value: object, /) -> builtins.bool: ...

@overload # == 1-d & object_
def __iter__(self: ndarray[tuple[int], dtype[object_]], /) -> Iterator[Any]: ...
@overload # == 1-d
def __iter__(self: ndarray[tuple[int], dtype[_ScalarT]], /) -> Iterator[_ScalarT]: ...
# NOTE: This weird `Never` tuple works around a strange mypy issue where it assigns
# `tuple[int]` to `tuple[Never]` or `tuple[int, int]` to `tuple[Never, Never]`.
# This way the bug only occurs for 9-D arrays, which are probably not very common.
@overload
def __iter__(self: ndarray[tuple[Never, Never, Never, Never, Never, Never, Never, Never, Never]], /) -> Iterator[Any]: ...
@overload # == 1-d & dtype[T \ object_]
def __iter__(self: ndarray[tuple[int], dtype[_NonObjectScalarT]], /) -> Iterator[_NonObjectScalarT]: ...
@overload # >= 2-d
def __iter__(self: ndarray[tuple[int, int, *tuple[int, ...]], dtype[_ScalarT]], /) -> Iterator[NDArray[_ScalarT]]: ...
@overload # ?-d
Expand Down
10 changes: 10 additions & 0 deletions numpy/typing/tests/data/reveal/ndarray_misc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function-based counterpart in `../from_numeric.py`.

"""

from collections.abc import Iterator
import ctypes as ct
import operator
from types import ModuleType
Expand All @@ -29,6 +30,10 @@ AR_m: npt.NDArray[np.timedelta64]
AR_U: npt.NDArray[np.str_]
AR_V: npt.NDArray[np.void]

AR_f8_1d: np.ndarray[tuple[int], np.dtype[np.float64]]
AR_f8_2d: np.ndarray[tuple[int, int], np.dtype[np.float64]]
AR_f8_3d: np.ndarray[tuple[int, int, int], np.dtype[np.float64]]

ctypes_obj = AR_f8.ctypes

assert_type(AR_f8.__dlpack__(), CapsuleType)
Expand Down Expand Up @@ -235,3 +240,8 @@ assert_type(AR_m.to_device("cpu"), npt.NDArray[np.timedelta64])

assert_type(f8.__array_namespace__(), ModuleType)
assert_type(AR_f8.__array_namespace__(), ModuleType)

assert_type(iter(AR_f8), Iterator[Any]) # any-D
assert_type(iter(AR_f8_1d), Iterator[np.float64]) # 1-D
assert_type(iter(AR_f8_2d), Iterator[npt.NDArray[np.float64]]) # 2-D
assert_type(iter(AR_f8_3d), Iterator[npt.NDArray[np.float64]]) # 3-D
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy