Incorrect type parameter scope inference with nested classes

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

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

評価

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

調査の方向性

まず、報告にあるネストした Foo[T]Bar[R] の例で mypy test_mypy.py を実行し、その後、ネストしたクラス本体に対する型パラメーターのスコープ処理を追跡します。外側の T が内側の R とともに認識されることを示す回帰テストを追加し、name-defined エラーが表示されなくなったことを確認します。

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

説明

bug topic-pep-695

Bug Report

In a class whose body is nested in another, mypy doesn't recognize the type parameters of the outer class as bound in the inner.

To Reproduce

# test_mypy.py
class Foo[T]:
    class Bar[R]:
        def foobar(self, t: T, r: R, /) -> None: ...
        print(T, R)

Output of program

T R

Note that there is no NameError being thrown. The names T and R are indeed both bound in the scope of the definition of R.

Actual Behavior

test_mypy.py:3: error: Name "T" is not defined  [name-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used:
  • Command used: mypy test_mypy.py
  • Mypy configuration options from mypy.ini (and other config files): None relevant
  • Python version used: 3.12, 3.13, 3.14
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

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

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

はじめの一歩

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

python/mypy のほかの issue

python/mypy の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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