From b583cd92d88841aa1a7780c17929d08ec03f66d7 Mon Sep 17 00:00:00 2001 From: Bhuvansh855 Date: Mon, 18 May 2026 20:44:48 +0530 Subject: [PATCH 1/2] Clarify generic type parameter ordering --- docs/spec/generics.rst | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/docs/spec/generics.rst b/docs/spec/generics.rst index c1fa05325..6d397f06a 100644 --- a/docs/spec/generics.rst +++ b/docs/spec/generics.rst @@ -505,6 +505,51 @@ That ``Child`` definition is equivalent to:: class Child(Parent1[T1, T3], Parent2[T2, T3], Generic[T1, T3, T2]): ... +When ``Generic[...]`` is present in the base class list, the order of +type parameters is determined by the order of type arguments to +``Generic``:: + + from typing import Generic, TypeVar + + T1 = TypeVar('T1') + T2 = TypeVar('T2') + + class Base(Generic[T1, T2]): + ... + + class Child(Base[T2, T1], Generic[T1, T2]): + ... + +In this example, ``Child`` has type parameter order ``T1``, ``T2``. + +Similarly, when a parameterized ``Protocol[...]`` base class is present, +the order of type parameters is determined by the order of type +arguments to ``Protocol``. A bare ``Protocol`` base class does not +affect type parameter ordering:: + + from typing import Protocol, TypeVar + + T1 = TypeVar('T1') + T2 = TypeVar('T2') + + class ProtoBase(Protocol[T1, T2]): + ... + + class ProtoChild(ProtoBase[T2, T1], Protocol[T1, T2]): + ... + + class BareProtoChild(ProtoBase[T1, T2], Protocol): + ... + +In both examples above, the type parameter order is ``T1``, ``T2``. + +For classes using the Python 3.12 generic class syntax, the type +parameter order is determined by the order of parameters in the type +parameter list:: + + class NewStyle[T1, T2]: + ... + A type checker should report an error when the type variable order is inconsistent:: From 8cd14d97186c9f459e26b9acf7629d48fea5c5a4 Mon Sep 17 00:00:00 2001 From: Bhuvansh855 Date: Thu, 21 May 2026 20:12:59 +0530 Subject: [PATCH 2/2] Refine type parameter ordering section --- docs/spec/generics.rst | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/docs/spec/generics.rst b/docs/spec/generics.rst index 6d397f06a..c258b8138 100644 --- a/docs/spec/generics.rst +++ b/docs/spec/generics.rst @@ -484,6 +484,16 @@ Also consider the following example:: In this case ``MyDict`` has a single type parameter, ``T``. +Type parameter ordering +----------------------- + +For classes using the Python 3.12 generic class syntax, the type +parameter order is determined by the order of parameters in the type +parameter list:: + + class NewStyle[T1, T2]: + ... + Type variables are applied to the defined class in the order in which they first appear in any generic base classes:: @@ -541,14 +551,7 @@ affect type parameter ordering:: class BareProtoChild(ProtoBase[T1, T2], Protocol): ... -In both examples above, the type parameter order is ``T1``, ``T2``. - -For classes using the Python 3.12 generic class syntax, the type -parameter order is determined by the order of parameters in the type -parameter list:: - - class NewStyle[T1, T2]: - ... +In all of the examples above, the type parameter order is ``T1``, ``T2``. A type checker should report an error when the type variable order is inconsistent::