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

unused-variable and undefined-variable with walrus operator #8425

Open
spagh-eddie opened this issue Mar 10, 2023 · 1 comment
Open

unused-variable and undefined-variable with walrus operator #8425

spagh-eddie opened this issue Mar 10, 2023 · 1 comment
Labels
Assignment expression Related to the walrus operator / assignment expression Decorators False Positive 🦟 A message is emitted but nothing is wrong with the code

Comments

@spagh-eddie
Copy link
Contributor

spagh-eddie commented Mar 10, 2023

Bug description

The closest open issue I could find was #7538, but this has different error codes

Curiously, if I put the offending line outside of the decorator, there is no error

def decorate(x):
    def wrapped(f):
        print(x)
        return f()
    return wrapped

original = {"key": "value", "this": "that"}

@decorate((d := original.copy()) and d.pop("key") and d)  # pylint error
def g(): print("g")
% python test.py
{'this': 'that'}
g

Configuration

No response

Command used

pylint test.py --disable=C

Pylint output

************* Module test
t.py:9:37: E0602: Undefined variable 'd' (undefined-variable)
t.py:9:54: E0602: Undefined variable 'd' (undefined-variable)
t.py:9:11: W0612: Unused variable 'd' (unused-variable)

------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)

Expected behavior

no error

Pylint version

pylint 2.17.0
astroid 2.15.0
Python 3.10.5 (main, Jul 29 2022, 12:30:46) [Clang 13.0.0 (clang-1300.0.27.3)]

OS / Environment

% uname
Darwin

Additional dependencies

No response

@spagh-eddie spagh-eddie added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Mar 10, 2023
@nickdrozd nickdrozd added Decorators False Positive 🦟 A message is emitted but nothing is wrong with the code Assignment expression Related to the walrus operator / assignment expression and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Mar 10, 2023
@kalekundert
Copy link

In case another example is useful, it seems like you can trigger this same false-positive via pytest. Note that a decorator is involved again:

$ cat test_mwe.py
import pytest

@pytest.mark.parametrize("list_1, list_2", [([a := 1], [a])])
def test_compare(list_1, list_2):
    assert list_1 == list_2
$ pytest test_mwe.py
============================= test session starts ==============================
platform linux -- Python 3.10.0, pytest-7.2.0, pluggy-1.0.0
rootdir: /home/kale
plugins: cov-4.0.0, env-0.6.2, subtests-0.7.0, xdoctest-1.1.1, hypothesis-6.39.4, devtools-0.12.2, unordered-0.4.1, hydra-core-1.3.2, pytest_tmp_files-0.0.0, xonsh-0.12.1, anyio-3.7.0, xdist-2.5.0, forked-1.4.0, typeguard-4.0.0
collected 1 item

test_mwe.py .                                                            [100%]

============================== 1 passed in 0.03s ===============================
$ pylint test_mwe.py --disable=C
************* Module test_mwe
test_mwe.py:3:56: E0602: Undefined variable 'a' (undefined-variable)
test_mwe.py:3:46: W0612: Unused variable 'a' (unused-variable)

------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)
$ pylint --version
pylint 3.0.2
astroid 3.0.1
Python 3.10.0 (default, Oct 20 2021, 17:23:57) [Clang 12.0.1 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Assignment expression Related to the walrus operator / assignment expression Decorators False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

No branches or pull requests

3 participants