-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Support for nonlocal declaration #504
Conversation
@@ -108,7 +108,7 @@ def __init__(self, fnam: str, errors: Errors, pyversion: int, | |||
self.errors.set_file('<input>') | |||
|
|||
def parse(self, s: str) -> MypyFile: | |||
self.tok = lex.lex(s) | |||
self.tok = lex.lex(s, pyversion = self.pyversion) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nit: redundant spaces around =
Looks good! This fixes one of the biggest remaining Python syntax incompatibility issues. Some notes above. Also, there should be a test case for type checking code using nonlocal (to test that assignment to variable defined using nonlocal uses the inferred type from the outer scope rather than inferring a completely new type). |
Thanks for the quick review. I'll fix the issues and add some test cases.
|
Conflicts: mypy/semanal.py mypy/treetransform.py
Thanks for addressing the issues! I'm going to merge this now. |
Support for nonlocal declaration
This PR implements #494.
There is one note I should make: An error message is generated when
nonlocal x
follows a definition ofx
, like in this case:However, Python 3.4 reports just a warning. I made this choice, because it was the easiest way to report an error (as it should) when there is a
nonlocal
declaration of a variable with the same name as a function parameter:I don't think it is such a bad idea to report an error in the first case too. In the first code snippet, the output is
2
. Which is (at least to me) a bit counter intuitive as thenonlocal x
apparently affects an earlier statement. Furthermore, generating an error message in this case does not exclude any uses ofnonlocal
as it can be moved to the front of the function with changing the semantics.Nevertheless, if you feel this is overly restrictive, let me know and I will try to find another way of dealing with this.