-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
mypy reports no issues on access of conditionally available fields #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 |
definitely not! There is a difference though. In the quoted example above, the type is inferred through type narrowing and mypy's support for conditional types. In principle there's no reason for type narrowing to not support Your examples and arguments has made it clear (to me at least) that the original problem phrasing is imprecise. It's more precise to say that since mypy supports type narrowing via conditionals, then I expect that it fails when fields are conditionally available. I've updated the title and description to reflect this. |
after reading the documentation again, https://mypy.readthedocs.io/en/stable/type_narrowing.html#type-narrowing-expressions, I see the issue. There's no type narrowing in effect on this conditional of course. I think it would be nice if type narrowing were extended to detect fields that are only present on one side of a conditional, but I see now that this is a feature request and probably not a bug |
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: