python / python/mypy

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

オープン
#17,464 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

プラグインを有効にした mypy 1.10.1 を使い、addee.py の再現用コードと mypy_plugin.py のプラグイン実装から始めます。未定義の asdfjkl アノテーションと並行して、プラグインが追加した var 属性の処理を追跡します。[name-defined] 診断が残り、余計な [no-redef] 診断が消えれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。