Skip to content

Fix a crash when nonlocal is defined at module level #8737

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 4 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8735.other
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash when a ``nonlocal`` is defined at module-level.

Closes #8735
2 changes: 1 addition & 1 deletion pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2913,7 +2913,7 @@ def _check_self_cls_assign(self, node: nodes.Assign) -> None:
elt.name for elt in target.elts if isinstance(elt, nodes.AssignName)
)
scope = node.scope()
nonlocals_with_same_name = any(
nonlocals_with_same_name = node.scope().parent and any(
child for child in scope.body if isinstance(child, nodes.Nonlocal)
)
if nonlocals_with_same_name:
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/n/nonlocal_without_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ def inner():
myint += 1

return inner()


nonlocal APPLE # [nonlocal-without-binding]
APPLE = 42
1 change: 1 addition & 0 deletions tests/functional/n/nonlocal_without_binding.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ nonlocal-without-binding:29:8:29:18:func.other_func:nonlocal name b found withou
nonlocal-without-binding:35:8:35:18:func.other_func2:nonlocal name c found without binding:HIGH
nonlocal-without-binding:40:4:40:14:SomeClass:nonlocal name x found without binding:HIGH
nonlocal-without-binding:43:8:43:26:SomeClass.func:nonlocal name some_attr found without binding:HIGH
nonlocal-without-binding:74:0:74:14::nonlocal name APPLE found without binding:HIGH