-
Notifications
You must be signed in to change notification settings - Fork 10
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
Codemod to simplify checks against empty sequences #212
Conversation
69f8b63
to
00f4616
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #212 +/- ##
==========================================
+ Coverage 96.43% 96.45% +0.02%
==========================================
Files 95 96 +1
Lines 4065 4095 +30
==========================================
+ Hits 3920 3950 +30
Misses 145 145
|
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.
I like this codemod and the implementation is nice. But we need to be a bit careful for the reasons described in the pylint rule:
Following this check blindly in weakly typed code base can create hard to debug issues. If the value can be something else that is falsey but not a sequence (for example
None
, an empty string, or0
) the code will not be equivalent.
It would be safer if we could somehow verify that the object was the same type but that's probably not going to be possible in general.
@@ -0,0 +1,11 @@ | |||
Empty sequences in a boolean comparison expression are considered falsy so you can use implicit boolean comparisons instead of | |||
comparing against empty sequences directly |
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.
Nitpick but there shouldn't be a line break here.
@@ -0,0 +1,11 @@ | |||
Empty sequences in a boolean comparison expression are considered falsy so you can use implicit boolean comparisons instead of |
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.
Empty sequences in a boolean comparison expression are considered falsy so you can use implicit boolean comparisons instead of | |
Empty sequences in Python always evaluate to `False`. This means that comparison expressions that use empty sequences can sometimes be simplified. In these cases no explicit comparison is required: instead we can rely on the [truth value](https://docs.python.org/3/library/stdtypes.html#truth-value-testing) of the object under comparison. This is sometimes referred to as "implicit" comparison. Using implicit boolean comparison expressions is considered best practice and can lead to better code. |
DESCRIPTION = ( | ||
"Replace comparisons to empty sequence with implicit boolean comparison." | ||
) | ||
REFERENCES: list = [] |
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.
): | ||
NAME = "fix-empty-sequence-comparison" | ||
SUMMARY = "Replace Comparisons to Empty Sequence with Implicit Boolean Comparison" | ||
REVIEW_GUIDANCE = ReviewGuidance.MERGE_WITHOUT_REVIEW |
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 needs to be MERGE_AFTER_REVIEW
since this has the potential to break existing code.
You're right. Ideally we would be able to limit this check to cases of value we're checking being sets themselves, but that's difficult. Unless we've already somehow solved the problem of "what is the underlying value of x?" Maybe we have it and I can rely on it @andrecsilva ?
and not cases:
|
We have |
This is certainly tricky. It's a good example of a codemod where an LLM might be able to provide more context/judgment. I'll take the blame for not evaluating the pylint rule carefully enough before giving the go-ahead for this work. I'm okay with accepting this codemod as-is (modulo comments above) under the condition that it is disabled by default. |
I'm not attached to this codemod and I always go for whatever is best for the product, so @drdavella let me know if you'd prefer:
|
@clavedeluna there is actually one other option: we define a variable called |
00f4616
to
bffa115
Compare
Quality Gate passedThe SonarCloud Quality Gate passed, but some issues were introduced. 3 New issues |
Overview
Codemod that changes expressions like
x =! []
orx == ()
in if, assert, assignment, or standalone statementsDescription