-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Closed
Labels
Description
Describe the issue:
At run time, adding or multiplying an ndarray with dtype np.float32 with a float returns an ndarray with dtype np.float32. However, mypy infers that it returns an npt.NDArray[Any] or npt.NDArray[np.float64].
Run time output of code example:
Runtime type is 'ndarray'
[0. 2. 4. 6.] float32
Runtime type is 'ndarray'
[1. 3. 5. 7.] float32
Reproduce the code example:
import numpy as np
import numpy.typing as npt
from typing import assert_type, reveal_type
x = np.array([0, 1, 2, 3], np.float32)
m = 2.0
b = 1.0
y1 = m * x
reveal_type(y1)
assert_type(y1, npt.NDArray[np.float32])
print(y1, y1.dtype)
y2 = y1 + b
reveal_type(y2)
assert_type(y2, npt.NDArray[np.float32])
print(y2, y2.dtype)Error message:
test.py:10: note: Revealed type is "numpy.ndarray[builtins.tuple[builtins.int, ...], numpy.dtype[numpy.floating[Any]]]"
test.py:11: error: Expression is of type "ndarray[tuple[int, ...], dtype[floating[Any]]]", not "ndarray[tuple[int, ...], dtype[floating[_32Bit]]]" [assert-type]
test.py:15: note: Revealed type is "numpy.ndarray[builtins.tuple[builtins.int, ...], numpy.dtype[numpy.float64]]"
test.py:16: error: Expression is of type "ndarray[tuple[int, ...], dtype[float64]]", not "ndarray[tuple[int, ...], dtype[floating[_32Bit]]]" [assert-type]
Found 2 errors in 1 file (checked 1 source file)Python and NumPy Versions:
numpy 2.2.5
Python 3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]
Type-checker version and settings:
mypy 1.15.0
Command line:
mypy test.py
mypy.ini:
[mypy]
plugins = numpy.typing.mypy_plugin
Additional typing packages.
mypy_extensions 1.1.0
typing_extensions 4.13.2