@@ -44,28 +44,92 @@ But the now concrete scalar types will no longer accept *any* `#!py floating[T]`
44
44
45
45
Type parameters can instead use an abstract scalar type as an upper bound. So instead of
46
46
47
+ /// tab | Python 3.10 and above
48
+
47
49
``` 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 ]: ...
49
68
```
50
69
70
+ ///
71
+
51
72
you can write
52
73
74
+ /// tab | Python 3.10 and above
75
+
53
76
``` 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
+
54
92
def f[FloatT: np.floating](x: FloatT) -> FloatT: ...
55
93
```
56
94
95
+ ///
96
+
57
97
As you can see, this also makes the code more readable.
58
98
59
99
But what if that isn't possible? For instance, you might have the following function:
60
100
101
+ /// tab | Python 3.10 and above
102
+
61
103
``` 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 ]: ...
63
111
```
64
112
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
+
65
126
In that case, you can rewrite it by using
66
127
[ ` #!py typing.overload ` ] ( https://typing.python.org/en/latest/spec/overload.html ) :
67
128
68
129
``` py
130
+ import numpy as np
131
+ from typing import overload
132
+
69
133
@overload
70
134
def f (x : np.complex64) -> np.float32: ...
71
135
@overload
0 commit comments