python / python/mypy

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

Open
#12,907 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-dataclasses topic-runtime-semantics topic-variable-scope
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report with the provided test.py example, then inspect commit 03901ef657daf6f477e6fe933c42ac270cf5034b and the dataclass-transform changes it introduced. Trace why the annotation on collections affects the later annotation and verify that the example no longer produces a false-positive name-defined error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.