Skip to content

Commit

Permalink
Fix false-positive used-before-assignment in function returns
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored and Pierre-Sassoulas committed Apr 7, 2021
1 parent 8f84dec commit 628c266
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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?
===========================
Expand Down
7 changes: 6 additions & 1 deletion pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,12 @@ def _is_variable_violation(
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)
)
Expand Down
25 changes: 16 additions & 9 deletions tests/functional/a/assignment_expression.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -16,6 +17,15 @@
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

Expand Down Expand Up @@ -53,7 +63,7 @@ def func():


# 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
Expand All @@ -70,10 +80,7 @@ def func():
)


# 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}'
7 changes: 3 additions & 4 deletions tests/functional/a/assignment_expression.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 628c266

Please sign in to comment.