Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

false-positive name-defined regression with dataclasses and shadowing #12907

Open
The-Compiler opened this issue May 30, 2022 · 2 comments
Open
Labels
bug mypy got something wrong topic-dataclasses topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope

Comments

@The-Compiler
Copy link
Contributor

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 03901ef ("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
@The-Compiler The-Compiler added the bug mypy got something wrong label May 30, 2022
@AlexWaygood AlexWaygood added topic-dataclasses topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope labels May 30, 2022
@AlexWaygood
Copy link
Member

Looks similar to #10640. But it's not the same, because #10640 reproduces on 0.950, whereas this doesn't. That might be because #10640 involves a from foo import bar import, whereas this is an import foo import.

@JukkaL
Copy link
Collaborator

JukkaL commented May 30, 2022

I suspect that the original behavior was accidental. Now dataclass behavior is similar to ordinary classes, since #12762 made dataclasses a little less special:

import collections

class Shadow:
    collections: collections.deque
    other: collections.defaultdict  # Error here

This is still a regression. As a workaround, you can import collections with a different name:

import collections as _collections

class Shadow:
    collections: _collections.deque
    other: _collections.defaultdict  # OK

I'm going to try to find a simple fix, but I think that we can live with this regression if there is no easy fix in sight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-dataclasses topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-variable-scope
Projects
None yet
Development

No branches or pull requests

3 participants