TypeVar bound doesn't understand CRTP
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
Bug Report
Ideally, this snippet would work. It enables making multiple types that share the same implementation but don't subclass each other (like newtype in other languages). If you comment out the other.quack() call and the bound="Foo[DerivedT]") then it will typecheck -- the issue is specifically with being able to express a typevar bound indicating the typevar has a base class that uses that typevar as a generic parameter.
#!/usr/bin/env python
from __future__ import annotations
from typing import Generic, TypeVar
DerivedT = TypeVar("DerivedT", bound="Foo[DerivedT]")
class Foo(Generic[DerivedT]):
def quack(self) -> None:
print("quack!")
def quack_together(self, other: DerivedT) -> None:
self.quack()
other.quack()
class Bar(Foo[Bar]): pass
x = Bar()
y = Bar()
x.quack_together(y)
$ python -m mypy --strict /tmp/self_test.py
self_test.py:6: error: Type variable "self_test.DerivedT" is unbound [valid-type]
self_test.py:6: note: (Hint: Use "Generic[DerivedT]" or "Protocol[DerivedT]" base class to bind "DerivedT" inside a class)
self_test.py:6: note: (Hint: Use "DerivedT" in function signature to bind "DerivedT" inside a function)
Found 1 error in 1 file (checked 1 source file)
- Mypy version used: mypy 1.10.0 (compiled: yes)
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.12
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
El reproducer está en /tmp/self_test.py; empieza ejecutando python -m mypy --strict /tmp/self_test.py con el límite CRTP mostrado. Investiga cómo mypy informa de la TypeVar no vinculada y considera el trabajo terminado cuando el ejemplo pase la comprobación de tipos sin ese error, preservando la relación de tipos descrita.
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
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100