@@ -35,7 +35,7 @@ Here is a very simple generic class that represents a stack:
3535
3636 .. note ::
3737
38- The type parameter syntax (e.g., ``class Foo[T]: ``) was introduced in Python 3.12.
38+ The type parameter syntax (e.g., ``class Foo[T]: ``) is available in Python 3.12 and newer .
3939 For earlier Python versions, generic classes need to be defined using
4040 ``TypeVar `` and ``Generic ``, as shown below.
4141
@@ -83,17 +83,14 @@ Defining subclasses of generic classes
8383**************************************
8484
8585User-defined generic classes and generic classes defined in :py:mod: `typing `
86- can be used as a base class for another class (generic or non-generic). For example:
86+ can be used as base classes for other classes (generic or non-generic). For example:
8787
8888.. code-block :: python
8989
90- from typing import Generic, TypeVar, Mapping, Iterator
91-
92- KT = TypeVar(' KT' )
93- VT = TypeVar(' VT' )
90+ from typing import Mapping, Iterator
9491
9592 # This is a generic subclass of Mapping
96- class MyMap (Mapping[KT , VT ]):
93+ class MyMap[ KT , VT ] (Mapping[KT , VT ]):
9794 def __getitem__ (self , k : KT ) -> VT : ...
9895 def __iter__ (self ) -> Iterator[KT ]: ...
9996 def __len__ (self ) -> int : ...
@@ -124,33 +121,16 @@ can be used as a base class for another class (generic or non-generic). For exam
124121 protocols like :py:class: `~typing.Iterable `, which use
125122 :ref: `structural subtyping <protocol-types >`.
126123
127- :py:class: `Generic <typing.Generic> ` can be omitted from bases if there are
128- other base classes that include type variables, such as ``Mapping[KT, VT] ``
129- in the above example. If you include ``Generic[...] `` in bases, then
130- it should list all type variables present in other bases (or more,
131- if needed). The order of type variables is defined by the following
132- rules:
133-
134- * If ``Generic[...] `` is present, then the order of variables is
135- always determined by their order in ``Generic[...] ``.
136- * If there are no ``Generic[...] `` in bases, then all type variables
137- are collected in the lexicographic order (i.e. by first appearance).
138-
139124For example:
140125
141126.. code-block :: python
142127
143- from typing import Generic, TypeVar, Any
144-
145- T = TypeVar(' T' )
146- S = TypeVar(' S' )
147- U = TypeVar(' U' )
128+ from typing import Any
129+ class One[T]: ...
130+ class Another[T]: ...
148131
149- class One (Generic[T]): ...
150- class Another (Generic[T]): ...
151-
152- class First (One[T], Another[S]): ...
153- class Second (One[T], Another[S], Generic[S, U, T]): ...
132+ class First[T, S](One[T], Another[S]): ...
133+ class Second[S, U, T](One[T], Another[S]): ...
154134
155135 x: First[int , str ] # Here T is bound to int, S is bound to str
156136 y: Second[int , str , Any] # Here T is Any, S is int, and U is str
@@ -218,8 +198,12 @@ the class definition.
218198
219199.. code-block :: python
220200
221- # T is the type variable bound by this class
222- class PairedBox (Generic[T]):
201+ from typing import TypeVar
202+
203+ S = TypeVar(' S' )
204+
205+ # T is the type parameter bound by this class
206+ class PairedBox[T]:
223207 def __init__ (self , content : T) -> None :
224208 self .content = content
225209
0 commit comments