Skip to content
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

Merged
merged 8 commits into from
Dec 3, 2014
Merged

Conversation

spkersten
Copy link
Contributor

This PR implements #494.

There is one note I should make: An error message is generated when nonlocal x follows a definition of x, like in this case:

def outer():
    x = 1
    def inner():
        x = 2
        nonlocal x
    inner()
    print(x)

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:

def outer():
    x = 1
    def inner(x):
        nonlocal x

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 the nonlocal x apparently affects an earlier statement. Furthermore, generating an error message in this case does not exclude any uses of nonlocal 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.

@@ -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)
Copy link
Collaborator

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 =

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 1, 2014

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).

@spkersten
Copy link
Contributor Author

Thanks for the quick review. I'll fix the issues and add some test cases.

"there should be a test case for type checking", that was a subtle way of saying that it doesn't work yet :-). I thought that my changes in semanal.py would be enough, because that is what happens for global. Turns out it is not enough and there is a bug for global. I'll try to fix both.

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 3, 2014

Thanks for addressing the issues! I'm going to merge this now.

JukkaL added a commit that referenced this pull request Dec 3, 2014
Support for nonlocal declaration
@JukkaL JukkaL merged commit ed32d9a into python:master Dec 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants