Open
Description
openedon 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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment