Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TYP: mypy infers that adding/multiplying a npt.NDArray[np.float32] with a float promotes dtype to Any or np.float64 #28805

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

Closed
bkeryan opened this issue Apr 23, 2025 · 1 comment · Fixed by #28807
Assignees

Comments

@bkeryan
Copy link

bkeryan commented Apr 23, 2025

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

@jorenham
Copy link
Member

jorenham commented Apr 23, 2025

m * x returns an array of floating[Any] dtype. That's valid, because it is fully compatible with float32. So I don't consider ndarray.__mul__ to be incorrect here.

The return type of y1 + b, on the other hand, seems to indeed be incorrect: NDArray[floating].__add__ should return NDArray[floating] when passed a float; but it instead returns NDArray[float64], which is incorrect.

Here's the minimal (.pyi) reproducer:

import numpy as np
import numpy.typing as npt

y1: npt.NDArray[np.floating]
m: float

reveal_type(y1 + m)  # npt.NDArray[np.float64]

Thanks for reporting this, I'll look into fixing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants