From c119955b81b08103ab36ce53cb65d97bcf99b602 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Wed, 15 Jun 2022 19:21:05 -0400 Subject: [PATCH] Emit `used-before-assignment` when calling nested functions before assignment --- doc/whatsnew/2/2.15/index.rst | 4 ++++ pylint/checkers/variables.py | 5 +++-- tests/functional/u/used/used_before_assignment.py | 7 +++++++ tests/functional/u/used/used_before_assignment.txt | 1 + 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/doc/whatsnew/2/2.15/index.rst b/doc/whatsnew/2/2.15/index.rst index 0b6ed8b1d0..d800b1c67d 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 + Other bug fixes =============== diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 6fcb576c52..094e2f9eb6 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