False positive typing.Self has incompatible type when method uses class' generic type
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Merge medio
- 1 d 18 h
- PR fusionados (30 d)
- 54
Descripción
Bug Report
mypy seems to get confused about type of typing.Self, when class is also typing.Generic, inside methods if a parameter has
the same type as the class Generic's TypeVar.
To Reproduce
from collections.abc import Awaitable, Callable
from typing import Generic, Self, TypeVar
AttributeValueT = TypeVar("AttributeValueT")
class Attribute(Generic[AttributeValueT]):
def __init__(self, on_changed: Callable[[Self], Awaitable[None]]) -> None:
self._on_changed = on_changed
async def bar(self, _: AttributeValueT) -> None:
# Argument 1 has incompatible type "Attribute[AttributeValueT]"; expected "Self" [arg-type]
await self._on_changed(self) # type: ignore[arg-type]
Expected Behavior
I expect self / Self to have the correct type and to be able to avoid # type: ignore[arg-type]
Actual Behavior
See mypy error is in method comment above; error only makes sense with code as context.
Your Environment
- Mypy version used: mypy 1.8.0 (compiled: yes)
- Python version used: 3.11.7
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
Empieza ejecutando el reproductor proporcionado con mypy 1.8.0 y confirma el error de tipo incompatible relacionado con typing.Self y el AttributeValueT genérico. Sigue el tratamiento de Self para los métodos de las clases Generic y después añade una prueba de regresión que muestre que self se acepta sin type ignore y que la suite de pruebas pasa.
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
- 42/100