Skip to content

Emit used-before-assignment when calling nested functions before assignment #6959

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 16, 2022
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
4 changes: 4 additions & 0 deletions doc/whatsnew/2/2.15/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ False negatives fixed

Closes #5653

* Emit ``used-before-assignment`` when calling nested functions before assignment.

Closes #6812

* Emit ``used-before-assignment`` for self-referencing assignments under if conditions.

Closes #6643
Expand Down
5 changes: 3 additions & 2 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,12 @@ class C: ...
return False
if isinstance(frame, nodes.FunctionDef):
# If the parent of the current node is a
# function, then it can be under its scope
# (defined in, which doesn't concern us) or
# function, then it can be under its scope (defined in); or
# the `->` part of annotations. The same goes
# for annotations of function arguments, they'll have
# their parent the Arguments node.
if frame.parent_of(defframe):
return node.lineno < defframe.lineno
if not isinstance(node.parent, (nodes.FunctionDef, nodes.Arguments)):
return False
elif any(
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/u/used/used_before_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@
MSG = "hello %s" % MSG # [used-before-assignment]

MSG2 = "hello %s" % MSG2 # [used-before-assignment]

def outer():
inner() # [used-before-assignment]
def inner():
pass

outer()
1 change: 1 addition & 0 deletions tests/functional/u/used/used_before_assignment.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
used-before-assignment:6:19:6:22::Using variable 'MSG' before assignment:HIGH
used-before-assignment:8:20:8:24::Using variable 'MSG2' before assignment:HIGH
used-before-assignment:11:4:11:9:outer:Using variable 'inner' before assignment:HIGH