You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
np.float64 is not accepted as float under static type checks #21906
# Python version 3.8.10, numpy version 1.23.0
import numpy as np
x = np.float64(1.0)
print(f"x={x} of type={type(x)} is float: {isinstance(x, float)}")
# x=1.0 of type=<class 'numpy.float64'> is float: True
def f(a: float) -> float:
return a
f(x)
"""
pyright 1.1.256:
error: Argument of type "float64" cannot be assigned to parameter "a" of type "float" in function "f"
"float64" is incompatible with "float" (reportGeneralTypeIssues)
mypy 0.961:
error: Argument 1 to "f" has incompatible type "floating[_64Bit]"; expected "float"
"""
Replacing np.float64 with np.float_ or np.double does not help.
Reacted by Yue JIN, Satyajeet Kanetkar, Joe Maliksi, Brooks Smith, Gayan Nagoda, Yasuhiro Fujita, Yuri Kilochek, Joren Hammudoglu, Moritz Wolter, Allan Leal and 3 more
Reacted by Pieter Eendebak, Stephan Troyer, Simon M. and Allan Leal
The bit issue here is that with current structure of the np.float64 annotations, i.e. it bring treated as parametrised np.floating type alias, that we cannot use different base classes for the likes of np.float64 and np.float32, the latter not inheriting from float during runtime. Replacing the type alias with a proper subclass is quite frankly not a viable solution, as thus would make it impractically difficult to do anything with all but a limited set of np.number subtype.
So this leaves us with two options:
Maintain the status quo, i.e. don’t treat np.float64 as a float subtype.
Treat float as a np.floating subtype, even though this is not the case during runtime for the likes or np.float32. I recall there being discussion on this subject in the past; it might be worthwhile to reopen those.
Activity
BvB93 commentedon Jul 6, 2022
The bit issue here is that with current structure of the
np.float64annotations, i.e. it bring treated as parametrised np.floating type alias, that we cannot use different base classes for the likes ofnp.float64andnp.float32, the latter not inheriting fromfloatduring runtime. Replacing the type alias with a proper subclass is quite frankly not a viable solution, as thus would make it impractically difficult to do anything with all but a limited set ofnp.numbersubtype.So this leaves us with two options:
np.float64as afloatsubtype.floatas anp.floatingsubtype, even though this is not the case during runtime for the likes ornp.float32. I recall there being discussion on this subject in the past; it might be worthwhile to reopen those.gerner commentedon Sep 29, 2022
I ran into this when I upgraded from numpy 1.21.x to 1.22.x. Is there a simple workaround to unblock static checks?
satyanash commentedon Nov 18, 2022
This is quite weird and breaks static type-checkers on a bunch of code that treats
floatandnp.float64as equivalent.