python / python/typing

Spec: More precision on type parameter order

Aperta
#2,059 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic: documentation topic: typing spec
Lingua principale
Python
Stelle
1.8k
Fork
302
Merge medio
23h
PR unite (30g)
8

Descrizione

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 to Generic.
  • If the class has Protocol[...] as a syntactic base, the order is the order of the type arguments to Protocol. This applies only if Protocol is subscripted, not if the class inherits from bare Protocol.
  • 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".

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla sezione “Arbitrary generic types as base classes” della specifica dei generics, in particolare dalla regola attuale sull’ordinamento delle variabili di tipo. Confronta quella formulazione con i casi proposti nell’issue per PEP 695, Generic, Protocol e le classi base. Il lavoro è completato quando la specifica definisce esplicitamente l’ordinamento per ogni caso e corrisponde agli esempi forniti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.