-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[red-knot] model that class/comprehension bodies run immediately #13933
Comments
Should we do the same for list/dict/set comprehensions? (Generators expressions obviously do not run immediately, unlike list/dict/set comprehensions.) |
Yes! |
See #13879 for discussion of generator expressions as it relates to this task. |
@dcreager is this complete now? |
I think it is. |
I'm looking at how we're representing the bindings snapshots next, to see if there's any performance we can squeeze out of it. But I'm 👍 to closing this since the functionality is there and working. |
Currently, we have a general strategy for name lookups in enclosing scopes: they use the type as seen from end of the scope where it's defined. For example:
For nested functions, this is a pretty reasonable default behavior. Another option would be to consider all definitions of
x
(or at least all definitions ofx
visible from the point wheref
is defined, forwards). This would result in revealingLiteral[1] | Literal["foo"]
forx
in the inner scope off
, instead. This would be safer, but also more annoying for the more common case wheref
is not called from the module body. It would also mean we would always considerx
possibly-undefined insidef
, iff
is defined beforex
, which could be really annoying.We could try to be smarter by detecting, for example, that the function
f
is (or might be) called from module level, and only consider earlier definitions if we observe this possibility. But this will always be best-effort, since reliably detecting all possible paths (other functions, even possibly in other modules) through whichf
could be called from module level isn't feasible.But there are cases where we can be quite sure that the nested scope will run before the end of the outer scope. For example, class definitions:
The class body always runs immediately, so it should resolve types in the enclosing scope from the class definition's point in control flow, not from end-of-scope.
The text was updated successfully, but these errors were encountered: