8000 DOC: add release note for the ``numpy.typing.NBitBase`` deprecation · numpy/numpy@8176a27 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8176a27

Browse files
committed
DOC: add release note for the numpy.typing.NBitBase deprecation
1 parent d69a9e5 commit 8176a27

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
``numpy.typing.NBitBase`` deprecation
2+
-------------------------------------
3+
:obj:`numpy.typing.NBitBase` is deprecated and will be removed in a future
4+
version.
5+
6+
This type was previously intended to be used as a generic upper bound for
7+
type-parameters:
8+
9+
.. code-block:: python
10+
11+
import numpy as np
12+
import numpy.typing as npt
13+
14+
def f[NT: npt.NBitBase](x: np.complexfloating[NT]) -> np.floating[NT]: ...
15+
16+
However, due to changes in NumPy 2.2 where `float64` and `complex128` were
17+
changed to concrete subtypes of `floating` and `complexfloating`, this usage
18+
pattern now causes type compatibility issues. For example, `floating[_64Bit]`
19+
is no longer assignable to `float64`, causing type-checkers to reject valid
20+
code like `x: float64 = f(complex128(1))`.
21+
22+
**Alternative approach using `typing.overload`:**
23+
24+
.. code-block:: python
25+
26+
import numpy as np
27+
from typing import overload
28+
29+
@overload
30+
def f(x: np.complex64) -> np.float32: ...
31+
@overload
32+
def f(x: np.complex128) -> np.float64: ...
33+
@overload
34+
def f(x: np.clongdouble) -> np.longdouble: ...
35+
36+
While more verbose, this approach is more explicit and easier to understand.
37+
38+
**Simpler alternative for cases where input and output types are related:**
39+
40+
For situations where the input scalar type is the same as the output type,
41+
a more direct approach can be used:
42+
43+
Old:
44+
45+
.. code-block:: python
46+
47+
import numpy as np
48+
import numpy.typing as npt
49+
50+
def f[NT: npt.NBitBase](x: np.floating[NT]) -> np.floating[NT]: ...
51+
52+
New:
53+
54+
.. code-block:: python
55+
56+
import numpy as np
57+
58+
def f[FloatT: np.floating](x: FloatT) -> FloatT: ...
59+
60+
This new approach is not only more concise but also more readable.
61+
62+
Note: The compatibility issue with `NBitBase` becomes more critical as NumPy
63+
and its type system evolve. In NumPy's upcoming typing system, all scalar types
64+
will be proper subtypes rather than type aliases, making this deprecation
65+
necessary for future type checking accuracy.

0 commit comments

Comments
 (0)
0