TypeForm: Recognize type variables in a consistent way
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
Prerequisite: This issue applies after PR https://github.com/python/mypy/pull/19596 is merged.
Currently mypy sometimes allows a type variable to be assigned to a TypeForm variable and sometimes it does not. This is a problem because the behavior is inconsistent.
Specifically, mypy allows a type variable to be assigned to a TypeForm variable iff it is in a quoted type expression:
[case testTypeVarTypeFormsAreOnlyRecognizedInStringAnnotation]
...
E = TypeVar('E')
class Box(Generic[E]):
def foo(self, e: E) -> None:
list_of_typx: List[TypeForm] = [E] # E: "E" is a type variable and only valid in type context
typx1: TypeForm = E # E: "E" is a type variable and only valid in type context
typx2: TypeForm = 'E'
When a type variable is NOT quoted, mypy will not allow it to be assigned:
[case testEveryKindOfTypeExpressionIsAssignableToATypeFormVariable]
SomeTypeVar = TypeVar('SomeTypeVar')
typx: TypeForm
...
# NOTE: Unbound TypeVar isn't currently accepted as a TypeForm. Is that OK?
typx = SomeTypeVar # E: Incompatible types in assignment (expression has type "TypeVar", variable has type "TypeForm[Any]")
I'd argue that TypeForm should always accept a type variable because it is a valid type expression and TypeForms are intended to accept all kinds of type expressions:
def cast[T](type_expr: TypeForm[T], value_expr: object) -> T: ...
cast(SomeTypeVar, some_expression) # should not be an error
In summary:
- mypy should allow a type variable to be assigned to a TypeForm variable, regardless of whether it is quoted or not. Update above-referenced tests to reflect this new behavior.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Primero revisa el PR prerequisite #19596 y, después, localiza las pruebas llamadas testTypeVarTypeFormsAreOnlyRecognizedInStringAnnotation y testEveryKindOfTypeExpressionIsAssignableToATypeFormVariable. Comprueba cómo se gestionan las variables de tipo entrecomilladas y no entrecomilladas para las asignaciones de TypeForm. Se considera terminado cuando ambos casos permiten de forma coherente las variables de tipo y las pruebas reflejan el nuevo comportamiento.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 42/100