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 with inline if #7779

Closed
Zeckie opened this issue Nov 16, 2022 · 1 comment · Fixed by #8029
Closed

False positive used-before-assignment with inline if #7779

Zeckie opened this issue Nov 16, 2022 · 1 comment · Fixed by #8029
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@Zeckie
Copy link
Contributor

Zeckie commented Nov 16, 2022

Bug description

str(foo if (foo := "BAR") else "").lower()

Pylint gives error: foobar.py:1:4: E0601: Using variable 'foo' before assignment (used-before-assignment)

When run in python:

>>> str(foo if (foo := "BAR") else "").lower()
'bar'

Configuration

No response

Command used

pylint foobar.py

Pylint output

************* Module foobar
foobar.py:1:4: E0601: Using variable 'foo' before assignment (used-before-assignment)

Expected behavior

No errors

Pylint version

pylint 2.15.5
astroid 2.12.12
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)]

OS / Environment

Windows

Additional dependencies

No response

@Zeckie Zeckie added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Nov 16, 2022
@nickdrozd
Copy link
Contributor

Variations that don't raise the error:

# reverse if / else values
str("" if (foo := "BAR") else foo).lower()

# drop .lower()
str(foo if (foo := "BAR") else "")

# simple assignment
var = foo if (foo := "BAR") else ""

@nickdrozd nickdrozd added Bug 🪲 C: used-before-assignment Issues related to 'used-before-assignment' check and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Nov 17, 2022
@Pierre-Sassoulas Pierre-Sassoulas added the Needs PR This issue is accepted, sufficiently specified and now needs an implementation label Nov 17, 2022
@clavedeluna clavedeluna added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Nov 23, 2022
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.15.10 milestone Jan 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: used-before-assignment Issues related to 'used-before-assignment' check False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants