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 for undefined variable with assignment expression #3688

Closed
hauntsaninja opened this issue Jun 14, 2020 · 6 comments · Fixed by #5213
Closed

False positive for undefined variable with assignment expression #3688

hauntsaninja opened this issue Jun 14, 2020 · 6 comments · Fixed by #5213
Labels
Assignment expression Related to the walrus operator / assignment expression False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.8
Milestone

Comments

@hauntsaninja
Copy link

hauntsaninja commented Jun 14, 2020

Steps to reproduce

# ~/tmp λ cat test4.py
def i(am: str = (the := "walrus")) -> str:
    return "goo goo g'joob"
print(the)

Current behavior

~/tmp λ pylint -E test4.py
************* Module test4
test4.py:5:6: E0602: Undefined variable 'the' (undefined-variable)

Expected behavior

pylint recognises that the is defined. (Or alternatively, pylint yells at me for doing this weird thing in the first place ;-) )

pylint --version output

~/tmp λ pylint --version
pylint 2.5.3
astroid 2.4.2
Python 3.8.3 (default, May 27 2020, 20:54:22) 
[Clang 11.0.3 (clang-1103.0.32.59)]

Pretty minor, especially given that assignment expressions aren't a priority for pylint (#3275 (comment)).

@bersbersbers
Copy link

bersbersbers commented Feb 5, 2021

Here's another case. Interestingly,

x = next(i for i in range(10) if (y := i * i))
print(y)

raises this false-positive, while

next(i for i in range(10) if (y := i * i))
print(y)

does not.

@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Feb 20, 2021
@egormm
Copy link

egormm commented Sep 22, 2021

A couple more interesting cases with false-positive behaviour:

print({'key': value if (value := 'something') else 'anything'})

raises used-before-assigment, while

value = val if (val := 'something') else 'anything'
print({'key': value})

doesn't. Excepting behaviour - no errors.
Same with classes and dataclasses:

class Dummy:
    def __init__(self, value):
        self.value = value

dummy = Dummy(value=val if (val := 'something') else 'anything')
print(dummy.value)

raises used-before-assigment, while

class Dummy:
    def __init__(self, value):
        self.value = value

value = val if (val := 'something') else 'anything'
dummy = Dummy(value)
print(dummy.value)

doesn't. Excepting behaviour - no errors.

Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Sep 22, 2021
Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Oct 16, 2021
@cdce8p cdce8p added python 3.8 Assignment expression Related to the walrus operator / assignment expression labels Oct 17, 2021
DanielNoord added a commit to DanielNoord/pylint that referenced this issue Oct 19, 2021
Pierre-Sassoulas pushed a commit to DanielNoord/pylint that referenced this issue Oct 25, 2021
Pierre-Sassoulas pushed a commit to DanielNoord/pylint that referenced this issue Oct 25, 2021
Pierre-Sassoulas added a commit that referenced this issue Oct 25, 2021
* Add tests for assignment expressions in function defaults

Ref #3688

* Upgrade astroid to 2.8.4

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
@DanielNoord DanielNoord added this to the 2.12.0 milestone Oct 25, 2021
@DanielNoord
Copy link
Collaborator

If have created a new issue for the last two issues of @egormm's comments, as they are related to a different message.

@hauntsaninja
Copy link
Author

@Pierre-Sassoulas @DanielNoord I don't think #5213 fixes the original case mentioned here (use of walrus in function default), should this issue be re-opened?

@DanielNoord
Copy link
Collaborator

It should be fixed with #5188. I separated this issue in two PR's as they covered different cases.

@hauntsaninja
Copy link
Author

Awesome, missed that, thank you so much!!

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 False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.8
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants