Allow Final attributes declared in class body if initialized in ctor
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
There is currently a safety check around TypeVar-dependent Final attributes declared in the class body that I believe we can loosen when the variable is assigned in the ctor. This should work analogously to non-TypeVar-dependent final attributes declared in the class body, which also require an assignment in the ctor (otherwise they emit "Final name must be initialized with a value").
- Are you reporting a bug, or opening a feature request? feature request
- Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.from typing import Final, Generic, TypeVar WidgetT = TypeVar('WidgetT', bound='Widget') class Widget: pass class WidgetHolder(Generic[WidgetT]): widget: Final[WidgetT] def __init__(self, widget: WidgetT) -> None: self.widget = widget - What is the actual behavior/output?
final.py: note: In class "WidgetHolder": final.py:13:5: error: Final name declared in class body cannot depend on type variables [misc] widget: Final[WidgetT] ^
In case anyone comes across this issue, the workaround is currently to not declare in the class body, but declare in the ctor with self.widget: Final[WidgetT] = widget instead. But just because a workaround exists doesn't mean we shouldn't try to fix this.
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 mypy en la reproducción proporcionada en final.py e inspecciona las comprobaciones existentes para los atributos Final declarados en el cuerpo de una clase, comparándolas con los atributos Final que no dependen de TypeVar. Se considera terminado cuando la reproducción pasa la comprobación de tipos al asignar el atributo en init, mientras que el error existente se mantiene para las declaraciones que no se inicializan allí.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 38/100