python / python/mypy

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

Open
#17,464 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in addee.py and the plugin implementation in mypy_plugin.py, using mypy 1.10.1 with the plugin enabled. Trace the handling of the plugin-added var attribute alongside the undefined asdfjkl annotation. Done means the [name-defined] diagnostic remains while the spurious [no-redef] diagnostic disappears.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.