Skip to content

Commit b583cd9

Browse files
committed
Clarify generic type parameter ordering
1 parent 9cd0a22 commit b583cd9

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

docs/spec/generics.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,51 @@ That ``Child`` definition is equivalent to::
505505
class Child(Parent1[T1, T3], Parent2[T2, T3], Generic[T1, T3, T2]):
506506
...
507507

508+
When ``Generic[...]`` is present in the base class list, the order of
509+
type parameters is determined by the order of type arguments to
510+
``Generic``::
511+
512+
from typing import Generic, TypeVar
513+
514+
T1 = TypeVar('T1')
515+
T2 = TypeVar('T2')
516+
517+
class Base(Generic[T1, T2]):
518+
...
519+
520+
class Child(Base[T2, T1], Generic[T1, T2]):
521+
...
522+
523+
In this example, ``Child`` has type parameter order ``T1``, ``T2``.
524+
525+
Similarly, when a parameterized ``Protocol[...]`` base class is present,
526+
the order of type parameters is determined by the order of type
527+
arguments to ``Protocol``. A bare ``Protocol`` base class does not
528+
affect type parameter ordering::
529+
530+
from typing import Protocol, TypeVar
531+
532+
T1 = TypeVar('T1')
533+
T2 = TypeVar('T2')
534+
535+
class ProtoBase(Protocol[T1, T2]):
536+
...
537+
538+
class ProtoChild(ProtoBase[T2, T1], Protocol[T1, T2]):
539+
...
540+
541+
class BareProtoChild(ProtoBase[T1, T2], Protocol):
542+
...
543+
544+
In both examples above, the type parameter order is ``T1``, ``T2``.
545+
546+
For classes using the Python 3.12 generic class syntax, the type
547+
parameter order is determined by the order of parameters in the type
548+
parameter list::
549+
550+
class NewStyle[T1, T2]:
551+
...
552+
508553
A type checker should report an error when the type variable order is
509554
inconsistent::
510555

0 commit comments

Comments
 (0)