Skip to content
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

Fix false-positive used-before-assignment in function returns #4307

Merged
merged 1 commit into from
Apr 7, 2021
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 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