summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Mueller <30130371+cdce8p@users.noreply.github.com>2021-04-07 00:37:16 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-04-07 18:56:50 +0200
commit628c266c0a2b0a0498d78d8a9a01704557e46c00 (patch)
tree701cb8b21baae8bd222d15cce41fe4b51736c293
parent8f84dec53d53b8297ef753277fc1d820becd8428 (diff)
downloadpylint-git-628c266c0a2b0a0498d78d8a9a01704557e46c00.tar.gz
Fix false-positive used-before-assignment in function returns
-rw-r--r--ChangeLog4
-rw-r--r--pylint/checkers/variables.py7
-rw-r--r--tests/functional/a/assignment_expression.py25
-rw-r--r--tests/functional/a/assignment_expression.txt7
4 files changed, 29 insertions, 14 deletions
diff --git a/ChangeLog b/ChangeLog
index be6c358c5..52aa04b5a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -22,6 +22,10 @@ Release date: Undefined
only contain ``__version__`` (also accessible with ``pylint.__version__``), other meta-information are still
accessible with ``import importlib;metadata.metadata('pylint')``.
+* Fix false-positive ``used-before-assignment`` in function returns.
+
+ Closes #4301
+
What's New in Pylint 2.7.5?
===========================
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index 3ad0ab883..66bd4fdd4 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -1457,7 +1457,12 @@ class VariablesChecker(BaseChecker):
and defnode.lineno == node.lineno
and isinstance(
defstmt,
- (astroid.Assign, astroid.AnnAssign, astroid.AugAssign),
+ (
+ astroid.Assign,
+ astroid.AnnAssign,
+ astroid.AugAssign,
+ astroid.Return,
+ ),
)
and isinstance(defstmt.value, astroid.JoinedStr)
)
diff --git a/tests/functional/a/assignment_expression.py b/tests/functional/a/assignment_expression.py
index aa6a27874..a2a586e53 100644
--- a/tests/functional/a/assignment_expression.py
+++ b/tests/functional/a/assignment_expression.py
@@ -1,5 +1,6 @@
"""Test assignment expressions"""
-# pylint: disable=missing-docstring,unused-argument,unused-import,invalid-name,blacklisted-name,unused-variable
+# pylint: disable=missing-docstring,unused-argument,unused-import,invalid-name
+# pylint: disable=blacklisted-name,unused-variable,pointless-statement
import re
if (a := True):
@@ -16,6 +17,15 @@ a = ["a ", "b ", "c "]
c = [text for el in a if (text := el.strip()) == "b"]
+# check wrong usage
+assert err_a, (err_a := 2) # [used-before-assignment]
+print(err_b and (err_b := 2)) # [used-before-assignment]
+values = (
+ err_c := err_d, # [used-before-assignment]
+ err_d := 2,
+)
+
+
# https://github.com/PyCQA/pylint/issues/3347
s = 'foo' if (fval := lambda: 1) is None else fval
@@ -53,7 +63,7 @@ print(function())
# https://github.com/PyCQA/pylint/issues/3763
-foo if (foo := 3 - 2) > 0 else 0 # [pointless-statement]
+foo if (foo := 3 - 2) > 0 else 0
# https://github.com/PyCQA/pylint/issues/4238
@@ -70,10 +80,7 @@ l3 += (
)
-# check wrong usage
-assert err_a, (err_a := 2) # [used-before-assignment]
-print(err_b and (err_b := 2)) # [used-before-assignment]
-values = (
- err_c := err_d, # [used-before-assignment]
- err_d := 2,
-)
+# https://github.com/PyCQA/pylint/issues/4301
+def func2():
+ return f'The number {(count := 4)} ' \
+ f'is equal to {count}'
diff --git a/tests/functional/a/assignment_expression.txt b/tests/functional/a/assignment_expression.txt
index 1be4c1389..c4b8da483 100644
--- a/tests/functional/a/assignment_expression.txt
+++ b/tests/functional/a/assignment_expression.txt
@@ -1,4 +1,3 @@
-pointless-statement:56:0::Statement seems to have no effect
-used-before-assignment:74:7::Using variable 'err_a' before assignment
-used-before-assignment:75:6::Using variable 'err_b' before assignment
-used-before-assignment:77:13::Using variable 'err_d' before assignment
+used-before-assignment:21:7::Using variable 'err_a' before assignment
+used-before-assignment:22:6::Using variable 'err_b' before assignment
+used-before-assignment:24:13::Using variable 'err_d' before assignment