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

False positive 'undefined-variable' with assignment expression in decorator #5735

Open
Jasha10 opened this issue Jan 28, 2022 · 1 comment
Open
Labels
Assignment expression Related to the walrus operator / assignment expression C: undefined-variable Issues related to 'undefined-variable' check Decorators False Positive 🦟 A message is emitted but nothing is wrong with the code

Comments

@Jasha10
Copy link

Jasha10 commented Jan 28, 2022

Bug description

When using an assignment expression in a decorator, a false positive 'undefined-variable' and 'unused-variable' may be raised.

Reproducible Example

# example.py
def preprocess(bar: str) -> str:
    return bar + "xyz"

def condition1(foo):
    return foo.startswith("abc")

def condition2(foo):
    return "bcdef" in foo

def postprocess(foo):
    return foo

data = ["abcdef", "qwerty"]

def decorator(arg):
    print(f"{arg=}")
    return lambda x: x

@decorator(
    [postprocess(foo) for string in data if condition1(foo := preprocess(string)) and condition2(foo)],
)
def decorated() -> None:
    pass

Pylint output

$ pylint example.py
************* Module example
tmp2.py:32:17: E0602: Undefined variable 'foo' (undefined-variable)
tmp2.py:32:97: E0602: Undefined variable 'foo' (undefined-variable)
tmp2.py:32:55: W0612: Unused variable 'foo' (unused-variable)

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

$ # Demonstrating that the python code runs as expected:
$ python example.py
arg=['abcdefxyz']

Expected behavior

No errors

Pylint version

$ pylint --version
pylint 2.12.2
astroid 2.9.3
Python 3.9.9 | packaged by conda-forge | (main, Dec 20 2021, 02:40:17)
[GCC 9.4.0]
@Jasha10 Jasha10 added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jan 28, 2022
@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.8 Assignment expression Related to the walrus operator / assignment expression and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling python 3.8 labels Jan 28, 2022
@DerBiasto

This comment was marked as off-topic.

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 C: undefined-variable Issues related to 'undefined-variable' check Decorators False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

No branches or pull requests

4 participants