-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
extend C1802 to detect len() comparisons with zero #10658
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
Open
Kaos599
wants to merge
6
commits into
pylint-dev:main
Choose a base branch
from
Kaos599:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e8e6180
extend C1802 to detect len() comparisons with zero
Kaos599 006b5d1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e0f7ada
ImplicitBooleanessChecker to flag len() comparisons in boolean contexts
Kaos599 8caba23
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 46439c9
expand use-implicit-booleaness-not-len to catch len() comparisons wi…
Kaos599 4aacb64
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,25 +14,25 @@ | |
| if True or len('TEST'): # [use-implicit-booleaness-not-len] | ||
| pass | ||
|
|
||
| if len('TEST') == 0: # Should be fine | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to check the initial reasoning for this before changing, I didn't have this context when labelling the initial issue a false negative. |
||
| if len('TEST') == 0: # [use-implicit-booleaness-not-len] | ||
| pass | ||
|
|
||
| if len('TEST') < 1: # Should be fine | ||
| if len('TEST') < 1: # [use-implicit-booleaness-not-len] | ||
| pass | ||
|
|
||
| if len('TEST') <= 0: # Should be fine | ||
| if len('TEST') <= 0: # [use-implicit-booleaness-not-len] | ||
| pass | ||
|
|
||
| if 1 > len('TEST'): # Should be fine | ||
| if 1 > len('TEST'): # [use-implicit-booleaness-not-len] | ||
| pass | ||
|
|
||
| if 0 >= len('TEST'): # Should be fine | ||
| if 0 >= len('TEST'): # [use-implicit-booleaness-not-len] | ||
| pass | ||
|
|
||
| if z and len('TEST') == 0: # Should be fine | ||
| if z and len('TEST') == 0: # [use-implicit-booleaness-not-len] | ||
| pass | ||
|
|
||
| if 0 == len('TEST') < 10: # Should be fine | ||
| if 0 == len('TEST') < 10: # Should be fine (chained comparison) | ||
| pass | ||
|
|
||
| # Should be fine | ||
|
|
@@ -73,16 +73,16 @@ | |
| while not len('TEST') and z: # [use-implicit-booleaness-not-len] | ||
| pass | ||
|
|
||
| assert len('TEST') > 0 # Should be fine | ||
| assert len('TEST') > 0 # [use-implicit-booleaness-not-len] | ||
|
|
||
| x = 1 if len('TEST') != 0 else 2 # Should be fine | ||
| x = 1 if len('TEST') != 0 else 2 # [use-implicit-booleaness-not-len] | ||
|
|
||
| f_o_o = len('TEST') or 42 # Should be fine | ||
|
|
||
| a = x and len(x) # Should be fine | ||
|
|
||
| def some_func(): | ||
| return len('TEST') > 0 # Should be fine | ||
| return len('TEST') > 0 # [use-implicit-booleaness-not-len] | ||
|
|
||
| def github_issue_1325(): | ||
| l = [1, 2, 3] | ||
|
|
@@ -143,7 +143,7 @@ class ChildClassWithoutBool(ClassWithoutBool): | |
| pandas_df = pd.DataFrame() | ||
| if len(pandas_df): | ||
| print("this works, but pylint tells me not to use len() without comparison") | ||
| if len(pandas_df) > 0: | ||
| if len(pandas_df) > 0: # [use-implicit-booleaness-not-len] | ||
| print("this works and pylint likes it, but it's not the solution intended by PEP-8") | ||
| if pandas_df: | ||
| print("this does not work (truth value of dataframe is ambiguous)") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It seems highly likely that optimization or code deduplication can be found by re-using
check_compare_to_str_or_zerohere.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.
From what i understood i have added
_extract_comparison_operands()helper method that both_check_compare_to_str_or_zero()and_check_len_comparison_with_zero()now use.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 have issued new commits resolving this