Closed
Closed
Copy link
Description
Describe the issue:
As I understand type promotion rules, if np float type is multiplied/divided by Python float, then numpy float's precision takes priority and will be preserved.
Noticed that in typing it does work for multiplication but for the division float type is lost and replaced with floating[Any]
. Shouldn't in also preserve np.float64
, is it a bug?
As a workaround typing system can be tricked by multiplying by (1/float)
.
Also noticed that for np.float32
it's replaced with floating[Any]
in both cases.
Ping @jorenham just in case.
Reproduce the code example:
import numpy as np
import numpy.typing as npt
from typing import reveal_type
def calculate(arr: npt.NDArray[np.float64], unit_scale: float) -> None:
a = arr / unit_scale
reveal_type(a) # ndarray[tuple[int, ...], dtype[floating[Any]]]
print("a =", a)
print("a dtype:", a.dtype) # float64
b = arr * unit_scale
reveal_type(b) # ndarray[tuple[int, ...], dtype[float64]
print("b =", b)
print("b dtype:", b.dtype) # float64
c = arr * (1 / unit_scale)
reveal_type(c) # ndarray[tuple[int, ...], dtype[float64]
print("c =", c)
print("c dtype:", c.dtype) # float64
# Example usage:
data = np.array([10.0, 20.0, 30.0], dtype=np.float64)
scale = 0.01230130120310230123012030012310
calculate(data, scale)
Error message:
test.py
test.py:8:17 - information: Type of "a" is "ndarray[tuple[int, ...], dtype[floating[Any]]]"
test.py:13:17 - information: Type of "b" is "ndarray[tuple[int, ...], dtype[float64]]"
test.py:18:17 - information: Type of "c" is "ndarray[tuple[int, ...], dtype[float64]]"
0 errors, 0 warnings, 3 informations
Python and NumPy Versions:
2.2.4
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:
pyright 1.1.399
Additional typing packages.
No response
Update. Can confirm issue as resolved.