Description
Describe the issue:
When updating from numpy 2.2.6 to 2.3.0, I encountered a new type check error that I believe is incorrect.
When you iterate over a matrix, the elements should be the same shape as the original matrix, but with one dimension dropped. Because the NDArray
type has ambiguous shape, elements could either be an array OR a scalar.
However, in numpy 2.3.0, when I iterate over a matrix of type NDArray[np.float64]
, numpy typing says each element has type np.float64
. It is assuming that the matrix is 1-dimensional and each element is a scalar, which is incorrect.
arr: NDArray[np.float64] = np.zeros((5,3))
for element in arr:
# Each `element` is an `NDArray[np.float64]` of shape (3)
# But instead, numpy typing says it is a `float64`
In numpy 2.2.6, each element had type Any
- vague, but at least not incorrect.
From the release notes, I see that improvements were made to numpy's static typing in 2.3.0. I also see mypy_plugin
was deprecated, but I don't think that's the issue because I didn't have the plugin enabled in the first place.
So is this an actual bug, or am I misunderstanding how to use static typing with numpy? I feel a little shaky on the subject. If I am doing this wrong, please show me the correct way π
Reproduce the code example:
"""A simple example that should throw a type check error."""
import numpy as np
from numpy.typing import NDArray
def example(pixels: NDArray[np.float64]) -> None:
"""`pixels` is an NDArray of shape (length, 3)."""
# Convert NDArray to a list of tuples
tuples: list[tuple] = [tuple(pixel) for pixel in pixels]
Error message:
error: Argument 1 to "tuple" has incompatible type "float64"; expected "Iterable[Any]" [arg-type]
Python and NumPy Versions:
2.3.0
3.13.5 (main, Jun 12 2025, 22:47:08) [GCC 12.2.0]
Type-checker version and settings:
mypy version: 1.16.1
mypy command: mypy .
Additional typing packages.
No response