Skip to content

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

Open
@denballakh

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.py
def f() -> None:
    from typing_extensions import assert_type

    class X:
        pass

    # x = X() # note this line is commented out
    assert_type(x, X)


## b.py
from a import X
reveal_type(X)
# Revealed type is "def () -> a.X@5"

Second case:

## a.py
def f() -> None:
    from typing_extensions import assert_type

    class X:
        pass

    x = X() # note this line is not commented out
    assert_type(x, X)


## b.py
from a import X
reveal_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