Skip to content

Use of undefined variable causes unrelated local class to leak scope #13024

Description

@denballakh

Bug Report
I noticed a strange behavior and inconsistency when first argument of typing_extensions.assert_type is not defined.

To Reproduce
First case:

## a.pydeff() ->None:
fromtyping_extensionsimportassert_typeclassX:
pass# x = X() # note this line is commented outassert_type(x, X)
## b.pyfromaimportXreveal_type(X)
# Revealed type is "def () -> a.X@5"

Second case:

## a.pydeff() ->None:
fromtyping_extensionsimportassert_typeclassX:
passx=X() # note this line is not commented outassert_type(x, X)
## b.pyfromaimportXreveal_type(X)
# Module "a" has no attribute "X"# Revealed type is "Any"

To reproduce run mypy b.py.
This snippets differ by only one line x = X(), but mypy behavior is very different. It gives me module has no attribute error, or gives no error.

Expected Behavior
I expect to get the Module "a" has no attribute "X" error in both cases

My Environment

  • mypy 0.961 (compiled: no)
  • no command-line flags, no config file
  • CPython 3.10.4
  • Windows 10

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions