python / python/mypy

"error: Name is not defined" and confusing behavior on instance-level classes

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

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

priority-1-normal topic-error-reporting topic-usability
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Running mypy on the following example:

from typing import Type

class Foo:
    def __init__(self):
        class Bar:
            pass

        self.Bar: Type[Bar] = Bar

foo = Foo()
bar: foo.Bar = foo.Bar()

reveal_type(foo.Bar)
reveal_type(bar)

results in the following slightly confusing and seemingly contradictory output:

example.py:11: error: Name 'foo.Bar' is not defined  # on bar: foo.Bar
example.py:13: note: Revealed type is 'Type[example.Bar@5]'  # on reveal_type(foo.Bar)
example.py:14: note: Revealed type is 'Any'  # on reveal_type(bar)

Ideally, I would have expected reveal_type(bar) to reveal example.Bar@5. I'm not sure if there is a way to correctly type this code without resorting to # type: ignore right now.

Tested on mypy 0.761, Python 3.8.1.

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

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

はじめの一歩

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

調査の方向性

mypy を使って example.py の例を再現し、アノテーション bar: foo.Bar と reveal_type の呼び出しに焦点を当てます。mypy がインスタンスレベルのクラス参照をどのように処理するかを追跡し、報告されたエラーと Any の結果を期待される型 example.Bar@5 に置き換えられるかどうかを確認します。example.py が意図どおりに型チェックを通過し、reveal_type(bar) がそのクラス型を報告すれば完了です。

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

評価

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

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

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