Skip to content

Commit c0d814c

Browse files
committed
Use PEP 695 syntax consistently in generics examples and functions
1 parent 1726a1d commit c0d814c

1 file changed

Lines changed: 17 additions & 34 deletions

File tree

docs/reference/generics.rst

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,10 @@ where the types of the arguments or return value have some relationship:
145145

146146
.. code-block:: python
147147
148-
from typing import TypeVar, Sequence
149-
150-
T = TypeVar('T')
148+
from typing import Sequence
151149
152150
# A generic function!
153-
def first(seq: Sequence[T]) -> T:
151+
def first[T](seq: Sequence[T]) -> T:
154152
return seq[0]
155153
156154
As with generic classes, the type variable can be replaced with any
@@ -174,14 +172,12 @@ functions do not share any typing relationship to each other:
174172

175173
.. code-block:: python
176174
177-
from typing import TypeVar, Sequence
178-
179-
T = TypeVar('T')
175+
from typing import Sequence
180176
181-
def first(seq: Sequence[T]) -> T:
177+
def first[T](seq: Sequence[T]) -> T:
182178
return seq[0]
183179
184-
def last(seq: Sequence[T]) -> T:
180+
def last[T](seq: Sequence[T]) -> T:
185181
return seq[-1]
186182
187183
Variables should not have a type variable in their type unless the type variable
@@ -198,20 +194,16 @@ the class definition.
198194

199195
.. code-block:: python
200196
201-
from typing import TypeVar
202-
203-
S = TypeVar('S')
204-
205197
# T is the type parameter bound by this class
206198
class PairedBox[T]:
207199
def __init__(self, content: T) -> None:
208200
self.content = content
209201
210-
# S is a type variable bound only in this method
211-
def first(self, x: list[S]) -> S:
202+
# S is a type parameter bound only in this method
203+
def first[S](self, x: list[S]) -> S:
212204
return x[0]
213205
214-
def pair_with_first(self, x: list[S]) -> tuple[S, T]:
206+
def pair_with_first[S](self, x: list[S]) -> tuple[S, T]:
215207
return (x[0], self.content)
216208
217209
box = PairedBox("asdf")
@@ -225,12 +217,8 @@ methods:
225217

226218
.. code-block:: python
227219
228-
from typing import TypeVar
229-
230-
T = TypeVar('T', bound='Shape')
231-
232220
class Shape:
233-
def set_scale(self: T, scale: float) -> T:
221+
def set_scale[T: Shape](self: T, scale: float) -> T:
234222
self.scale = scale
235223
return self
236224
@@ -256,15 +244,13 @@ For class methods, you can also define generic ``cls``, using :py:class:`type`:
256244

257245
.. code-block:: python
258246
259-
from typing import Optional, TypeVar, Type
260-
261-
T = TypeVar('T', bound='Friend')
247+
from typing import Optional
262248
263249
class Friend:
264250
other: Optional["Friend"] = None
265251
266252
@classmethod
267-
def make_pair(cls: type[T]) -> tuple[T, T]:
253+
def make_pair[T: Friend](cls: type[T]) -> tuple[T, T]:
268254
a, b = cls(), cls()
269255
a.other = b
270256
b.other = a
@@ -455,25 +441,22 @@ It's therefore often useful to be able to limit the types that a type
455441
variable can take on, for instance, by restricting it to values that are
456442
subtypes of a specific type.
457443

458-
Such a type is called the upper bound of the type variable, and is specified
459-
with the ``bound=...`` keyword argument to :py:class:`~typing.TypeVar`.
444+
Such a type is called the upper bound of the type parameter. In modern Python
445+
(3.12+), this can be expressed directly in the type parameter list using
446+
``[T: Bound]`` syntax.
460447

461448
.. code-block:: python
462449
463-
from typing import TypeVar, SupportsAbs
450+
from typing import SupportsAbs
464451
465-
T = TypeVar('T', bound=SupportsAbs[float])
452+
def largest_in_absolute_value[T: SupportsAbs[float]](*xs: T) -> T:
453+
return max(xs, key=abs)
466454
467455
In the definition of a generic function that uses such a type variable
468456
``T``, the type represented by ``T`` is assumed to be a subtype of
469457
its upper bound, so the function can use methods of the upper bound on
470458
values of type ``T``.
471459

472-
.. code-block:: python
473-
474-
def largest_in_absolute_value(*xs: T) -> T:
475-
return max(xs, key=abs) # Okay, because T is a subtype of SupportsAbs[float].
476-
477460
In a call to such a function, the type ``T`` must be replaced by a
478461
type that is a subtype of its upper bound. Continuing the example
479462
above:

0 commit comments

Comments
 (0)