Skip to content

Commit 7b1242c

Browse files
Fix a crash when nonlocal is defined at module level (#8737) (#8742)
* Fix a crash when a ``nonlocal`` is defined at module-level. Closes #8735 (cherry picked from commit 33d3f22) Co-authored-by: Mark Byrne <[email protected]>
1 parent c1243cf commit 7b1242c

File tree

4 files changed

+9
-1
lines changed

4 files changed

+9
-1
lines changed

doc/whatsnew/fragments/8735.other

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash when a ``nonlocal`` is defined at module-level.
2+
3+
Closes #8735

pylint/checkers/variables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2851,7 +2851,7 @@ def _check_self_cls_assign(self, node: nodes.Assign) -> None:
28512851
elt.name for elt in target.elts if isinstance(elt, nodes.AssignName)
28522852
)
28532853
scope = node.scope()
2854-
nonlocals_with_same_name = any(
2854+
nonlocals_with_same_name = node.scope().parent and any(
28552855
child for child in scope.body if isinstance(child, nodes.Nonlocal)
28562856
)
28572857
if nonlocals_with_same_name:

tests/functional/n/nonlocal_without_binding.py

+4
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ def inner():
6969
myint += 1
7070

7171
return inner()
72+
73+
74+
nonlocal APPLE # [nonlocal-without-binding]
75+
APPLE = 42

tests/functional/n/nonlocal_without_binding.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ nonlocal-without-binding:29:8:29:18:func.other_func:nonlocal name b found withou
22
nonlocal-without-binding:35:8:35:18:func.other_func2:nonlocal name c found without binding:HIGH
33
nonlocal-without-binding:40:4:40:14:SomeClass:nonlocal name x found without binding:HIGH
44
nonlocal-without-binding:43:8:43:26:SomeClass.func:nonlocal name some_attr found without binding:HIGH
5+
nonlocal-without-binding:74:0:74:14::nonlocal name APPLE found without binding:HIGH

0 commit comments

Comments
 (0)