Spec: More precision on type parameter order
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 55/100
- Issue-Typ
- Dokumentation
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Veraltet
- Tech-Stack
- python
- Bereich
- documentation
Rechercherichtung
Beginne mit dem Abschnitt „Arbitrary generic types as base classes“ der Generics-Spezifikation, insbesondere mit seiner aktuellen Regel zur Reihenfolge von Typvariablen. Vergleiche diese Formulierung mit den im Issue vorgeschlagenen Fällen für PEP 695, Generic, Protocol und Basisklassen. Als erledigt gilt die Aufgabe, wenn die Spezifikation die Reihenfolge für jeden Fall ausdrücklich definiert und mit den angegebenen Beispielen übereinstimmt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
The spec currently isn't very precise on the order in which type parameters appear in a class. (Given a class C that is generic over type parameters T1 and T2, does C[int, str] mean that T1 is bound to int or that T2 is bound to int?)
I think the rule should be:
- If PEP 695 syntax is used, the order is the order in which the type parameters appear in the type parameter list.
- If the class has
Generic[...]as a syntactic base, the order is the order of the type arguments toGeneric. - If the class has
Protocol[...]as a syntactic base, the order is the order of the type arguments toProtocol. This applies only if Protocol is subscripted, not if the class inherits from bareProtocol. - Otherwise, the order is the order in which the type parameters appear syntactically in the base class list.
As far as I know, this is how all type checkers currently behave, but we just found out it's not how the runtime behaves when Protocol is involved: https://github.com/python/cpython/issues/137191#issuecomment-3132748382.
In these examples, all classes are generic over two parameters T1 and T2, and in all cases the type parameter order is T1, T2:
from typing import Generic, Protocol, TypeVar
class C1[T1, T2]: ... # order is T1, T2
T1 = TypeVar("T1")
T2 = TypeVar("T2")
class C2(C1[T2, T1], Generic[T1, T2]): ... # order is T1, T2
class GenericProto[T1, T2](Protocol): ... # order is T1, T2
class Proto2(GenericProto[T2, T1], Protocol[T1, T2]): ... # order is T1, T2
class Proto3(GenericProto[T1, T2], Protocol): ... # order is T1, T2
class C3(C2[T1, T2]): ... # order is T1, T2
We should change the spec to make this rule explicit. The only current rule appears to be in https://typing.python.org/en/latest/spec/generics.html#arbitrary-generic-types-as-base-classes "Type variables are applied to the defined class in the order in which they first appear in any generic base classes".
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/typing
-
topic: typing spec
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
-
topic: typing spec
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
topic: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
topic: documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
-
topic: conformance tests topic: typing spec
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 72/100
Ähnliche Issues
-
triage/confirmed
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
agentscope-ai/agentscope#2775 ·
-
comp/desktop P3 type/bug
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 92/100
NousResearch/hermes-agent#118866 ·
-
bug
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
apache/cloudstack#14222 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100