python / python/mypy

false-positive `name-defined` regression with dataclasses and shadowing

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

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

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

説明

Bug Report

This file:

from dataclasses import dataclass
import collections


@dataclass
class Shadow:

    collections: collections.deque
    other: collections.defaultdict


print(Shadow(collections.deque(), collections.defaultdict()))

is perhaps a bit unorthodox, but runs fine, because the collections: ... doesn't actually shadow the module when defining other.

Actual Behavior

mypy v0.950 was happy with it, but mypy v0.960 claims that:

test.py:9: error: Name "collections.defaultdict" is not defined

Introducing commit

Bisected to 03901ef657daf6f477e6fe933c42ac270cf5034b ("Running dataclass transform in a later pass to fix crashes (#12762)", @JukkaL)

Your Environment

  • Mypy version used: v0.960
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.4
  • Operating system and version: Archlinux

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

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

はじめの一歩

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

調査の方向性

提供されたtest.pyの例で報告内容を再現し、その後、コミット03901ef657daf6f477e6fe933c42ac270cf5034bと、それが導入したdataclass-transformの変更を調査してください。collectionsへのアノテーションが後続のアノテーションに影響する理由を追跡し、その例がfalse-positive name-definedエラーを生成しなくなっていることを確認してください。

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

評価

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

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

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