This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -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::
Expand All
@@ -505,6 +515,44 @@ 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 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::
Expand Down
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Clarify generic type parameter ordering #2289
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Clarify generic type parameter ordering #2289
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing