diff --git a/doc/whatsnew/2/2.15/index.rst b/doc/whatsnew/2/2.15/index.rst index 0cef6563dd..46fa821929 100644 --- a/doc/whatsnew/2/2.15/index.rst +++ b/doc/whatsnew/2/2.15/index.rst @@ -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 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 6b825c6259..13d289d72f 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -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( diff --git a/tests/functional/u/used/used_before_assignment.py b/tests/functional/u/used/used_before_assignment.py index 5b469041ea..2784663234 100644 --- a/tests/functional/u/used/used_before_assignment.py +++ b/tests/functional/u/used/used_before_assignment.py @@ -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() diff --git a/tests/functional/u/used/used_before_assignment.txt b/tests/functional/u/used/used_before_assignment.txt index 64fee9e556..b3aaa17ffd 100644 --- a/tests/functional/u/used/used_before_assignment.txt +++ b/tests/functional/u/used/used_before_assignment.txt @@ -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