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 (used-before-assignment) due to assignment expression within a tuple #3275

Closed
msladecek opened this issue Nov 27, 2019 · 2 comments · Fixed by #4064
Closed
Labels
Bug 🪲 Minor 💅 Polishing pylint is always nice

Comments

@msladecek
Copy link

Steps to reproduce

  1. Create file examples/tuple.py
values = (
    a := 1,
    b := 2,
    c := a + b,
)
print(values)

function = lambda: (
    a := 1,
    b := 2,
    c := a + b,
)
print(function())
  1. Run pylint
$ pylint examples/tuple.py
************* Module tuple
examples/tuple.py:4:9: E0601: Using variable 'a' before assignment (used-before-assignment)
examples/tuple.py:4:13: E0601: Using variable 'b' before assignment (used-before-assignment)
examples/tuple.py:11:9: E0602: Undefined variable 'a' (undefined-variable)
examples/tuple.py:11:13: E0602: Undefined variable 'b' (undefined-variable)

Current behavior

Pylint returns error E0601 (used-before-assignment) when the left side of an assignment expression is used within the same tuple it was created in.

Pylint returns error E0602 (undefined-variable) when the left side of an assignment expression is used within the same tuple it was created in, in a lambda.

Expected behavior

Pylint shouldn't detect E0601 & E0602 on these expressions because they are valid python3.8 code.

$ python examples/tuple.py
(1, 2, 3)
(1, 2, 3)

pylint --version output

pylint 2.4.4
astroid 2.3.3
Python 3.8.0 (default, Nov  2 2019, 00:46:27) 
[GCC 9.2.0]
@PCManticore PCManticore added Bug 🪲 Minor 💅 Polishing pylint is always nice labels Nov 27, 2019
@PCManticore
Copy link
Contributor

Thanks for the report. While assignment expressions is part of the language, supporting it is not a priority right now.

@bjtho08
Copy link

bjtho08 commented May 7, 2020

I also get Undefined variable 'match' in this otherwise elegant piece of code:

import os
from re import search
...
log_path = "/path/to/log/dir/
if any((match := search(r"\.csv$", file)) for file in os.listdir(log_path)):
        log_id = match.string

In my opinion this is a good example of how to properly utilize the assignment operator and it should not result in an obvious false positive, regardless of what some people might think of the assignment operator in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Minor 💅 Polishing pylint is always nice
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants