Unexpected `[no-redef]` errors for plugin-generated class replacement attributes if `[name-defined]` occurs in the same module
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 & To Reproduce
I have a class called Addee with an attribute (currently a method called var), which I want to replace with a plain variable (essentially: var: int). The buggy module and plugin implementation module is given below:
# addee.py
class _Addee: ...
class Addee(_Addee):
def var(self, /) -> None: ...
# a: asdfjkl # Uncomment to reproduce the bug in this report
# mypy_plugin.py
from __future__ import annotations
import typing_extensions as t
import mypy.plugin
import mypy.plugins.common
if t.TYPE_CHECKING:
import collections.abc as cx
def plugin(version: str) -> type[mypy.plugin.Plugin]:
return AttributeAddingPlugin
class AttributeAddingPlugin(mypy.plugin.Plugin):
@t.override
def get_base_class_hook(
self, fullname: str
) -> cx.Callable[[mypy.plugin.ClassDefContext], None] | None:
if fullname == "addee._Addee":
return base_class_hook
return None
def base_class_hook(ctx: mypy.plugin.ClassDefContext, /) -> None:
mypy.plugins.common.add_attribute_to_class(
ctx.api, ctx.cls, "var", ctx.api.named_type("builtins.int")
)
Expected Behavior
# addee.py
class _Addee: ...
class Addee(_Addee):
def var(self, /) -> None: ...
a: asdfjkl # mypy: Name "asdfjkl" is not defined [name-defined]
Actual Behaviour
An unexpected [no-redef] error additionally appears; type: ignore[name-defined] on a: asdfjkl does not suppress this. Comment out a: asdfjkl and the error disappears.
# addee.py
class _Addee: ...
class Addee(_Addee):
def var(self, /) -> None: ... # mypy: Name "var" already defined (possibly by an import) [no-redef]
a: asdfjkl # mypy: Name "asdfjkl" is not defined [name-defined]
Your Environment
- Mypy version used: 1.10.1
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None (apart fromplugins = mypy_plugin.py) - Python version used: 3.10
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 con el reproductor en addee.py y la implementación del plugin en mypy_plugin.py, usando mypy 1.10.1 con el plugin habilitado. Sigue el tratamiento del atributo var añadido por el plugin junto a la anotación asdfjkl no definida. Se considera terminado cuando el diagnóstico [name-defined] permanece y desaparece el diagnóstico espurio [no-redef].
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