Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented new check use-maxsplit-arg (#4469)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com> Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
- Loading branch information
1 parent
0978e09
commit 605afdb
Showing
7 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
"""Emit a message for accessing first/last element of string.split""" | ||
# pylint: disable=line-too-long,missing-docstring,unsubscriptable-object,too-few-public-methods,invalid-name,redefined-builtin | ||
|
||
# Test subscripting .split() | ||
get_first = '1,2,3'.split(',')[0] # [use-maxsplit-arg] | ||
get_last = '1,2,3'[::-1].split(',')[0] # [use-maxsplit-arg] | ||
|
||
SEQ = '1,2,3' | ||
get_first = SEQ.split(',')[0] # [use-maxsplit-arg] | ||
get_last = SEQ.split(',')[-1] # [use-maxsplit-arg] | ||
get_first = SEQ.rsplit(',')[0] # [use-maxsplit-arg] | ||
get_last = SEQ.rsplit(',')[-1] # [use-maxsplit-arg] | ||
|
||
# Don't suggest maxsplit=1 if not accessing the first or last element | ||
get_mid = SEQ.split(',')[1] | ||
get_mid = SEQ.split(',')[-2] | ||
|
||
|
||
# Test varying maxsplit argument -- all these will be okay | ||
# ## str.split() tests | ||
good_split = '1,2,3'.split(sep=',', maxsplit=1)[-1] | ||
good_split = '1,2,3'.split(sep=',', maxsplit=1)[0] | ||
good_split = '1,2,3'.split(sep=',', maxsplit=2)[-1] | ||
good_split = '1,2,3'.split(sep=',', maxsplit=2)[0] | ||
good_split = '1,2,3'.split(sep=',', maxsplit=2)[1] | ||
|
||
# ## str.rsplit() tests | ||
good_split = '1,2,3'.rsplit(sep=',', maxsplit=1)[-1] | ||
good_split = '1,2,3'.rsplit(sep=',', maxsplit=1)[0] | ||
good_split = '1,2,3'.rsplit(sep=',', maxsplit=2)[-1] | ||
good_split = '1,2,3'.rsplit(sep=',', maxsplit=2)[0] | ||
good_split = '1,2,3'.rsplit(sep=',', maxsplit=2)[1] | ||
|
||
|
||
# Tests on class attributes | ||
class Foo(): | ||
class_str = '1,2,3' | ||
def __init__(self): | ||
self.my_str = '1,2,3' | ||
|
||
def get_string(self) -> str: | ||
return self.my_str | ||
|
||
# Class attributes | ||
get_first = Foo.class_str.split(',')[0] # [use-maxsplit-arg] | ||
get_last = Foo.class_str.split(',')[-1] # [use-maxsplit-arg] | ||
get_first = Foo.class_str.rsplit(',')[0] # [use-maxsplit-arg] | ||
get_last = Foo.class_str.rsplit(',')[-1] # [use-maxsplit-arg] | ||
|
||
get_mid = Foo.class_str.split(',')[1] | ||
get_mid = Foo.class_str.split(',')[-2] | ||
|
||
|
||
# Test with accessors | ||
bar = Foo() | ||
get_first = bar.get_string().split(',')[0] # [use-maxsplit-arg] | ||
get_last = bar.get_string().split(',')[-1] # [use-maxsplit-arg] | ||
|
||
get_mid = bar.get_string().split(',')[1] | ||
get_mid = bar.get_string().split(',')[-2] | ||
|
||
|
||
# Test with iterating over strings | ||
list_of_strs = ["a", "b", "c", "d", "e", "f"] | ||
for s in list_of_strs: | ||
print(s.split(" ")[0]) # [use-maxsplit-arg] | ||
print(s.split(" ")[-1]) # [use-maxsplit-arg] | ||
print(s.split(" ")[-2]) | ||
|
||
|
||
# Test warning messages (matching and replacing .split / .rsplit) | ||
class Bar(): | ||
split = '1,2,3' | ||
|
||
# Error message should show Bar.split.split(',', maxsplit=1) or Bar.split.rsplit(',', maxsplit=1) | ||
print(Bar.split.split(",")[0]) # [use-maxsplit-arg] | ||
print(Bar.split.split(",")[-1]) # [use-maxsplit-arg] | ||
print(Bar.split.rsplit(",")[0]) # [use-maxsplit-arg] | ||
print(Bar.split.rsplit(",")[-1]) # [use-maxsplit-arg] | ||
|
||
# Special cases | ||
a = "1,2,3".split('\n')[0] # [use-maxsplit-arg] | ||
a = "1,2,3".split('split')[-1] # [use-maxsplit-arg] | ||
a = "1,2,3".rsplit('rsplit')[0] # [use-maxsplit-arg] |
This file contains 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use-maxsplit-arg:5:12::Use '1,2,3'.split(',', maxsplit=1)[0] instead | ||
use-maxsplit-arg:6:11::"Use '1,2,3'[::-1].split(',', maxsplit=1)[0] instead" | ||
use-maxsplit-arg:9:12::Use SEQ.split(',', maxsplit=1)[0] instead | ||
use-maxsplit-arg:10:11::Use SEQ.rsplit(',', maxsplit=1)[-1] instead | ||
use-maxsplit-arg:11:12::Use SEQ.split(',', maxsplit=1)[0] instead | ||
use-maxsplit-arg:12:11::Use SEQ.rsplit(',', maxsplit=1)[-1] instead | ||
use-maxsplit-arg:45:12::Use Foo.class_str.split(',', maxsplit=1)[0] instead | ||
use-maxsplit-arg:46:11::Use Foo.class_str.rsplit(',', maxsplit=1)[-1] instead | ||
use-maxsplit-arg:47:12::Use Foo.class_str.split(',', maxsplit=1)[0] instead | ||
use-maxsplit-arg:48:11::Use Foo.class_str.rsplit(',', maxsplit=1)[-1] instead | ||
use-maxsplit-arg:56:12::Use bar.get_string().split(',', maxsplit=1)[0] instead | ||
use-maxsplit-arg:57:11::Use bar.get_string().rsplit(',', maxsplit=1)[-1] instead | ||
use-maxsplit-arg:66:10::Use s.split(' ', maxsplit=1)[0] instead | ||
use-maxsplit-arg:67:10::Use s.rsplit(' ', maxsplit=1)[-1] instead | ||
use-maxsplit-arg:76:6::Use Bar.split.split(',', maxsplit=1)[0] instead | ||
use-maxsplit-arg:77:6::Use Bar.split.rsplit(',', maxsplit=1)[-1] instead | ||
use-maxsplit-arg:78:6::Use Bar.split.split(',', maxsplit=1)[0] instead | ||
use-maxsplit-arg:79:6::Use Bar.split.rsplit(',', maxsplit=1)[-1] instead | ||
use-maxsplit-arg:82:4::Use '1,2,3'.split('\n', maxsplit=1)[0] instead | ||
use-maxsplit-arg:83:4::Use '1,2,3'.rsplit('split', maxsplit=1)[-1] instead | ||
use-maxsplit-arg:84:4::Use '1,2,3'.split('rsplit', maxsplit=1)[0] instead |