-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
mypy reports no issues on access of undefined variable #18708
Comments
It's hard for a type checker to catch such things as these are runtime behaviour dependent. Here's another even simpler example: class C:
x = 1
del x
print(C.x) Deducing that My way of working around this is to always use a main function to avoid having optionally defined globals in my python file: def main():
...
if __name__ == '__main__':
main() |
At the very least ... well at least maybe it should, I don't know if the fallout from this wouldn't make it worth it. |
I think there is a crucial difference between the examples. In the I understand that the typechecker attempts to determine the dynamic scope in both cases and fails in both cases (and it's great that it attempts to determine this!). In the first example neither the dynamic nor the static scope contains the attribute, yet the code typechecks, so that feels definitely like a bug. Does that make sense?
That's a good workaround, just never write to the module namespace, thanks |
I don't think that's true. The last line in the file creates foo in the module scope of test_foo. Optionally, but it still may exist. |
If you mean to say that conditionally instantiated variables should not be assumed to exist in a scope, it'll break a lot more stuff. For example: if TYPE_CHECKING:
from module import MyClass def foo(x: int):
if x < 0:
abs_x = abs(x)
else:
return x
return abs_x |
Bug Report
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=d8a24e474f61a151f549728e14173383
Expected Behavior
mypy should report an error
the name
foo
is not guaranteed to be part of the scope inprint_foo
. E.g. the following code will breakActual Behavior
mypy outputs "Success: no issues found in 1 source file"
Your Environment
see playground link
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: