-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Emit used-before-assignment
for variables only defined under always false tests
#6677
Emit used-before-assignment
for variables only defined under always false tests
#6677
Conversation
@jacobtylerwalls This seems like it could benefit from pylint-dev/astroid#1189. Perhaps we should try and get that landed first? |
I agree that's a good use of effort to get that merged. Not sure I immediately see the utility here, but it's certainly possible after looking more closely! :D |
Well, if you don't see the usability than that's a clear point of review. The system introduced there should be "easily" extendable to also include |
I guess it just comes down to how much to use inference in the |
f7c45dd
to
ecef2b5
Compare
Pull Request Test Coverage Report for Build 3508970379
💛 - Coveralls |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Leaving notes for when I come back: many of the messages in the primer differ refer to this pattern: if magic:
magic_num = 1
...
if magic:
print(magic_num) # likely that nothing happened to `magic` in the meantime, so safe(ish) Maybe a loose comparison of if-node tests or the pending astroid constraints PR could help. |
I'm still in shock about how great the primer bot is. It's like developing pylint in easy mode. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Along the lines of #2835, this should probably be a new message 1 if magic:
magic_num = 1
...
if magic:
print(magic_num) # likely but not certain that nothing happened to `magic` in the meantime 2 # handle two possible values
if flag == "expected_val1":
magic_num = 1
elif flag == "expected_val2":
magic_num = 2
# no else ... so magic_num may not be defined ... but does this ever realistically happen?
print(magic_num) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if you think I should follow the idea below to reduce the size of the PR.
This comment has been minimized.
This comment has been minimized.
🤖 Effect of this PR on checked open source code: 🤖 Effect on pandas:
This comment was generated for commit 11e9b28 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests cases are on point, but the code is complicated (because the problem we're trying to solve is complicated). I feel like we need a generic solution in astroid, something like infer raising a different exception when the variable can be unassigned, maybe ? Or an assigned boolean attribute in nodes ? There is an a lot of check that require control flow, implementing control flow in each checkers is going to be a lot more work than doing that in astroid directly. What do you think ?
I just want to relate this to @DanielNoord's comment about the astroid constraint PR. I think we might be conflating two things. The current proposal in pylint-dev/astroid#1189 is to add constraints on inferred values. Here, we're not using inference. We don't care what values
I'm sympathetic to that, but another way to look at it would be this is a "soft-launch" of control flow, using just one message, where the pylint primer results look promising, and then as we iron out kinks, we can implement a similar approach in astroid later, and extend it for |
🤖 Effect of this PR on checked open source code: 🤖 Effect on music21:
Effect on pandas:
Effect on psycopg:
This comment was generated for commit be571d1 |
The code in psycopg looks very suspicious, it might be a genuine bug. But in pandas it seems like a false positive. |
🤖 Effect of this PR on checked open source code: 🤖 Effect on music21:
Effect on psycopg:
This comment was generated for commit 2ae32ec |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👌 Feel free to merge, I'm not super comfortable with the variables checker and I don't understand everything, But the tests and the primer make me pretty confident it won't create too much issues.
if (never_defined := False): | ||
pass | ||
print(never_defined) # [used-before-assignment] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a new false-positive. Assignment expressions are bound before
the conditional check. Thus this executes just fine and will print False
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Fixed in #7847.
Cases with NamedExpr and Starred relating to definitions under "always false" conditions added in pylint-dev#6677.
Cases with NamedExpr and Starred relating to definitions under "always false" conditions added in #6677
Type of Changes
Description
Closes #4913
Determine if a variable is defined only under a code path guarded by an IF statement that
astroid
can determine is always falsy. Emitused-before-assignment
if that variable is later accessed.