python / python/mypy

Unexpected `[no-redef]` errors for plugin-generated class replacement attributes if `[name-defined]` occurs in the same module

Offen
#17,464 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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 from plugins = mypy_plugin.py)
  • Python version used: 3.10

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem Reproducer in addee.py und der Plugin-Implementierung in mypy_plugin.py und verwende mypy 1.10.1 mit aktiviertem Plugin. Verfolge die Behandlung des vom Plugin hinzugefügten var-Attributs neben der undefinierten asdfjkl-Annotation. Als erledigt gilt die Aufgabe, wenn die [name-defined]-Diagnose bestehen bleibt und die überflüssige [no-redef]-Diagnose verschwindet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.