python / python/mypy

Annotation parsing incorrect when class scope defines same name as import

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

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

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

説明

Bug Report

Given:

import uuid
class Bar:
    uuid: uuid.UUID = uuid.uuid4()
reveal_type(Bar().uuid)

mypy reports no errors and will inform the user that the type of the attribute is uuid.UUID.

However this isn't strictly correct and without lazy annotations enabled this code will actually error at runtime:

  File ".../demo.py", line 3, in Bar
    uuid: uuid.UUID = uuid.uuid4()
AttributeError: 'UUID' object has no attribute 'UUID'

Ideally mypy would report the missing attribute (UUID) along with a mention of the name overlap between the type name and the attribute name.

Cross-linking: a related issue was raised against pyflakes (that the import appeared unused; https://github.com/PyCQA/pyflakes/issues/682; https://github.com/PyCQA/pyflakes/issues/648), though rejected on the grounds that this code is indeed invalid. Unfortunately when this code is alongside other code it is easy for there not to be any linter errors.

Your Environment

  • Mypy version used: 0.910, also tested 0.941
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none that matter
  • Python version used: 3.9
  • Operating system and version: Ubuntu 20.04

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

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

はじめの一歩

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

調査の方向性

提供された Python スニペットを、設定フラグなしで mypy を使用して実行し、問題を再現します。その後、クラス属性とインポートされた uuid に対するアノテーション名の解決を追跡します。missing-attribute および name-overlap 診断のカバレッジを追加し、実行時に無効な例に対する動作を検証します。

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

評価

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

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

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