python / python/mypy

Mypy gives confusing/misleading error message

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

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

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

説明

Bug Report

Mypy reports error: Name "..." is not defined [name-defined] when using an instance attribute that was annotated.

To Reproduce

from inspect import Parameter


class ListElementDescriptor[T]:
    name: str
    item_type: type[T]

    @property
    def signature_parameter(self) -> Parameter:
        return Parameter(name=self.name, kind=Parameter.KEYWORD_ONLY, annotation=list[self.item_type])

Expected Behavior

Definitely not an error that self.item_type is not defined. I suspect mypy tries to to convey a different error, maybe related to using variables for type parameters in generics, though I disagree with that as well since list[] is not used here in a typing context to specify a type, but is used as a value.

Actual Behavior

typing-7.py:11: error: Name "self.item_type" is not defined  [name-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.12.0+dev.6a0657e5959ba1777c4d427f8f355d499035d145 (compiled: no)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
enable_incomplete_feature = "NewGenericSyntax"
disable_bytearray_promotion = true
disable_memoryview_promotion = true
check_untyped_defs = true
warn_unreachable = true
warn_redundant_casts = true
warn_unused_ignores = true
  • Python version used: 3.12

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

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

はじめの一歩

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

調査の方向性

まず、提供された ListElementDescriptor の例を mypy 1.12.0+dev.6a0657e5959ba1777c4d427f8f355d499035d145 と、示されている Python 3.12 の設定で実行します。list[self.item_type] に対する診断を追跡し、self.item_type が未定義だと主張するのではなく、報告される問題が正確になるよう更新します。再現手順をもう一度実行して、誤解を招く name-defined エラーがなくなったことを確認します。

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

評価

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

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

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