python / python/typing

Spec: More precision on type parameter order

Abierto
#2,059 0 comentarios 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

topic: documentation topic: typing spec
Lenguaje dominante
Python
Estrellas
1.8k
Forks
302
Merge medio
23 h
PR fusionados (30 d)
8

Descripción

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".

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con la sección “Arbitrary generic types as base classes” de la especificación de generics, especialmente con su regla actual para el orden de las variables de tipo. Compara esa redacción con los casos propuestos en el issue para PEP 695, Generic, Protocol y las clases base. Se considera terminado cuando la especificación define explícitamente el orden para cada caso y coincide con los ejemplos proporcionados.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
documentation
Tipo de issue
Documentación
Dificultad
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
55/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.