Skip to content

Commit 27c7b31

Browse files
committed
📝 tabbed code examples for <3.12 and >=3.12
1 parent 5d06179 commit 27c7b31

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

docs/user_guide/differences.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,92 @@ But the now concrete scalar types will no longer accept *any* `#!py floating[T]`
4444

4545
Type parameters can instead use an abstract scalar type as an upper bound. So instead of
4646

47+
/// tab | Python 3.10 and above
48+
4749
```py
48-
def f[T: npt.NBitBase](x: np.floating[T]) -> np.floating[T]: ...
50+
import numpy as np
51+
import numpy.typing as npt
52+
from typing import TypeVar
53+
54+
NT = TypeVar("NT", bound=npt.NBitBase)
55+
56+
def f(x: np.floating[NT]) -> np.floating[NT]: ...
57+
```
58+
59+
///
60+
/// tab | Python 3.12 and above
61+
select: True
62+
63+
```py
64+
import numpy as np
65+
import numpy.typing as npt
66+
67+
def f[NT: npt.NBitBase](x: np.floating[NT]) -> np.floating[NT]: ...
4968
```
5069

70+
///
71+
5172
you can write
5273

74+
/// tab | Python 3.10 and above
75+
5376
```py
77+
import numpy as np
78+
from typing import TypeVar
79+
80+
FloatT = TypeVar("FloatT", bound=np.floating)
81+
82+
def f(x: FloatT) -> FloatT: ...
83+
```
84+
85+
///
86+
/// tab | Python 3.12 and above
87+
select: True
88+
89+
```py
90+
import numpy as np
91+
5492
def f[FloatT: np.floating](x: FloatT) -> FloatT: ...
5593
```
5694

95+
///
96+
5797
As you can see, this also makes the code more readable.
5898

5999
But what if that isn't possible? For instance, you might have the following function:
60100

101+
/// tab | Python 3.10 and above
102+
61103
```py
62-
def f[T: npt.NBitBase](x: np.complexfloating[T]) -> np.floating[T]: ...
104+
import numpy as np
105+
import numpy.typing as npt
106+
from typing import TypeVar
107+
108+
NT = TypeVar("NT", bound=npt.NBitBase)
109+
110+
def f(x: np.complexfloating[NT]) -> np.floating[NT]: ...
63111
```
64112

113+
///
114+
/// tab | Python 3.12 and above
115+
select: True
116+
117+
```py
118+
import numpy as np
119+
import numpy.typing as npt
120+
121+
def f[NT: npt.NBitBase](x: np.complexfloating[NT]) -> np.floating[NT]: ...
122+
```
123+
124+
///
125+
65126
In that case, you can rewrite it by using
66127
[`#!py typing.overload`](https://typing.python.org/en/latest/spec/overload.html):
67128

68129
```py
130+
import numpy as np
131+
from typing import overload
132+
69133
@overload
70134
def f(x: np.complex64) -> np.float32: ...
71135
@overload

0 commit comments

Comments
 (0)