Description
This is a breaking change in the public NumPy API. This change will affect many users, and a quick code search for npt.NBitBase
brings of 239 hits: https://github.com/search?q=npt.NBitBase+language%3APython&type=code&l=Python
But even so, this has to happen.
In NumType, all scalar types have their are annotated as concrete subclasses. Most function will no longer accepts floating[_32Bit]
, and require float32
instead. This is because floating[_32Bit]
is no longer assignable to float32
.
The purpose of numpy.typing.NBitBase
was as upper bound to a type parameter, for use in generic abstract scalar types like floating[T]
. But the now concrete scalar types will no longer accept any floating[T]
.
NBitBase
should therefore not be used anymore.
Type parameters can instead use an abstract scalar type as an upper bound. So instead of
def f[NBitT: npt.NBitBase](x: np.floating[NBitT]) -> np.floating[NBitT]: ...
you can write
def f[FloatT: np.floating](x: FloatT) -> FloatT: ...
If that isn't possible, for example with
def f[NBitT: npt.NBitBase](x: np.complexfloating[NBitT]) -> np.floating[NBitT]: ...
then typing.overload
can be used instead:
@overload
def f(x: np.complex64) -> np.float32: ...
@overload
def f(x: np.complex128) -> np.float64: ...
@overload
def f(x: np.clongdouble) -> np.longdouble: ...
See also https://numpy.org/doc/stable/reference/typing.html#numpy.typing.NBitBase