-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Use of undefined variable causes unrelated local class to leak scope #13024
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
Labels
Comments
Another reproducer: ## a.py
def f() -> None:
from typing_extensions import assert_type
# first case: X is importable from this file
class X:
a: A # error: name "A" is not defined
# second case: X is **not** importable from this file
# class X:
# pass
x = X()
assert_type(x, X)
## b.py
from a import X
reveal_type(X)
# in this file i get:
# in first case:
# note: Revealed type is "def () -> a.X@5"
# in second case:
# error:Module "a" has no attribute "X"
# note: Revealed type is "Any" |
Interesting, thanks for finding this. Note that it also repros with any use of E.g.: a.py
b.py
|
typing_extensions.assert_type
defines variable
Other reproducer, but without using undefined variables: ## a.py
from __future__ import annotations
def f() -> None:
class X:
z: Z # i guess, mypy thinks Z isnt defined and leaks all classes into global scope, but there are no errors
class Z: ...
a: int
t: type[X]
## b.py
from a import X, Z, a, t
# Module "a" has no attribute "a" # ok
# Module "a" has no attribute "t" # ok
reveal_type(X)
# Revealed type is "def () -> a.X@3" # not ok
reveal_type(Z)
# Revealed type is "def () -> a.Z@6" # not ok Note that it leaks only classes, not variables. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug Report
I noticed a strange behavior and inconsistency when first argument of
typing_extensions.assert_type
is not defined.To Reproduce
First case:
Second case:
To reproduce run
mypy b.py
.This snippets differ by only one line
x = X()
, but mypy behavior is very different. It gives memodule has no attribute
error, or gives no error.Expected Behavior
I expect to get the
Module "a" has no attribute "X"
error in both casesMy Environment
mypy 0.961 (compiled: no)
CPython 3.10.4
The text was updated successfully, but these errors were encountered: