Unexpected `[no-redef]` errors for plugin-generated class replacement attributes if `[name-defined]` occurs in the same module
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il riproduttore in addee.py e l’implementazione del plugin in mypy_plugin.py, usando mypy 1.10.1 con il plugin abilitato. Traccia la gestione dell’attributo var aggiunto dal plugin insieme all’annotazione asdfjkl non definita. Il lavoro è completato quando la diagnostica [name-defined] rimane e la diagnostica spuriosa [no-redef] scompare.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100