Skip to content

New semantic analyzer: add test case for redefined nonlocal variable #7089

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

Merged
merged 2 commits into from
Jun 28, 2019

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Jun 28, 2019

Closes #7060.

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I have couple comments.

def main():
bar = [] # type: typing.List[int]

def foo():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any difference in behavior if these functions are annotated (i.e. checked)? Maybe we should have two tests (with and without annotations)?


def foo():
nonlocal bar
bar = [] # type: typing.List[int]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be an error, because this is essentially the same as:

bar: List[int]
bar: List[int]  # Name 'bar' already defined

Moreover:

>>> import typing
>>> 
>>> def main():
...     bar: typing.List[int] = []
...     def g():
...         nonlocal bar
...         bar: typing.List[int] = []
  File "<stdin>", line 5
SyntaxError: annotated name 'bar' can't be nonlocal

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It generates an error if the functions are annotated. Updated test case to also cover this.

@JukkaL JukkaL merged commit 7bd951a into master Jun 28, 2019
@JelleZijlstra JelleZijlstra deleted the semanal-new-test branch June 28, 2019 13:18
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.

"UnboundLocalError" on an annotation for a nonlocal variable
2 participants