Bad error message surrounding generic type aliases being `Any` when it's really just unbound
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
from __future__ import annotations
from typing import Generic, TypeVar, ClassVar, Any
T = TypeVar("T")
class A(Generic[T]):
@classmethod
def bar(cls: type[A[int]]) -> None:
...
A.bar() # error: Invalid self argument "Type[A[Any]]" to attribute function "bar" with type "Callable[[Type[A[int]]], None]"
A_Alias1 = A
A_Alias1.bar() # error: Invalid self argument "Type[A[Any]]" to attribute function "bar" with type "Callable[[Type[A[int]]], None]"
reveal_type(A_Alias1) # note: Revealed type is "def [T] () -> __main__.A[T`1]"
A_Alias2 = A[Any]
A_Alias2.bar() # valid
reveal_type(A_Alias2) # note: Revealed type is "def () -> __main__.A[Any]"
def foo(a: type[A[int]]) -> None:
...
foo(A) # no error
foo(A_Alias1) # error: Argument 1 to "foo" has incompatible type "Type[A[Any]]"; expected "Type[A[int]]"
Invalid self argument "Type[A[Any]]" to attribute function "bar" with type "Callable[[Type[A[int]]], None]"
This error message is confusing, because it's implying it's Any when it's actually not, it looks like the error message is wrong.
I would think an error message like Invalid self argument "Type[A[T]]" to attribute function "bar" with type "Callable[[Type[A[int]]], None]" would be more accurate.
Also, with the foo example, why is it only complaining on the type alias and not on the raw usage? This is inconsistent with the class method example
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
Comienza ejecutando la reproducción del issue con mypy y compara los diagnósticos para la clase genérica sin procesar, A_Alias1, A_Alias2 y foo. Rastrea el manejo implicado de la visualización de tipos y de los alias genéricos; se considera terminado cuando el diagnóstico distingue correctamente una variable de tipo no vinculada de Any y el comportamiento indicado es coherente en todos los ejemplos.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 30/100