Skip to content
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

actively discourage or prevent the usage of numpy.typing.NBitBase #395

Closed
jorenham opened this issue Mar 24, 2025 · 0 comments · Fixed by #425
Closed

actively discourage or prevent the usage of numpy.typing.NBitBase #395

jorenham opened this issue Mar 24, 2025 · 0 comments · Fixed by #425
Labels

Comments

@jorenham
Copy link
Member

jorenham commented Mar 24, 2025

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

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

Successfully merging a pull request may close this issue.

1 participant