Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
115 additions
and
125 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
77 changes: 77 additions & 0 deletions
77
tests/functional/c/consider/consider_using_maxsplit_arg.py
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,77 @@ | ||
"""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] # [consider-using-maxsplit-arg] | ||
get_last = '1,2,3'[::-1].split(',')[0] # [consider-using-maxsplit-arg] | ||
|
||
SEQ = '1,2,3' | ||
get_first = SEQ.split(',')[0] # [consider-using-maxsplit-arg] | ||
get_last = SEQ.split(',')[-1] # [consider-using-maxsplit-arg] | ||
get_first = SEQ.rsplit(',')[0] # [consider-using-maxsplit-arg] | ||
get_last = SEQ.rsplit(',')[-1] # [consider-using-maxsplit-arg] | ||
|
||
get_mid = SEQ.split(',')[1] # This is okay | ||
get_mid = SEQ.split(',')[-2] # This is okay | ||
|
||
|
||
# Test varying maxsplit argument -- all these will be okay | ||
## str.split() tests | ||
good_split = '1,2,3'.split(sep=',', maxsplit=1)[-1] # This is fine | ||
good_split = '1,2,3'.split(sep=',', maxsplit=1)[0] # This is fine | ||
good_split = '1,2,3'.split(sep=',', maxsplit=2)[-1] # This is fine | ||
good_split = '1,2,3'.split(sep=',', maxsplit=2)[0] # This is fine | ||
good_split = '1,2,3'.split(sep=',', maxsplit=2)[1] # This is fine | ||
|
||
## str.rsplit() tests | ||
good_split = '1,2,3'.rsplit(sep=',', maxsplit=1)[-1] # This is fine | ||
good_split = '1,2,3'.rsplit(sep=',', maxsplit=1)[0] # This is fine | ||
good_split = '1,2,3'.rsplit(sep=',', maxsplit=2)[-1] # This is fine | ||
good_split = '1,2,3'.rsplit(sep=',', maxsplit=2)[0] # This is fine | ||
good_split = '1,2,3'.rsplit(sep=',', maxsplit=2)[1] # This is fine | ||
|
||
|
||
# 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] # [consider-using-maxsplit-arg] | ||
get_last = Foo.class_str.split(',')[-1] # [consider-using-maxsplit-arg] | ||
get_first = Foo.class_str.rsplit(',')[0] # [consider-using-maxsplit-arg] | ||
get_last = Foo.class_str.rsplit(',')[-1] # [consider-using-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] # [consider-using-maxsplit-arg] | ||
get_last = bar.get_string().split(',')[-1] # [consider-using-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]) # [consider-using-maxsplit-arg] | ||
print(s.split(" ")[-1]) # [consider-using-maxsplit-arg] | ||
print(s.split(" ")[-2]) | ||
|
||
|
||
# Test warning messages (matching and replacing .split / .rsplit) | ||
class Bar(): | ||
split = '1,2,3' | ||
|
||
print(Bar.split.split(",")[0]) # [consider-using-maxsplit-arg] (Error message should show Bar.split.partition) | ||
print(Bar.split.split(",")[-1]) # [consider-using-maxsplit-arg] (Error message should show Bar.split.rpartition) | ||
print(Bar.split.rsplit(",")[0]) # [consider-using-maxsplit-arg] (Error message should show Bar.split.partition) | ||
print(Bar.split.rsplit(",")[-1]) # [consider-using-maxsplit-arg] (Error message should show Bar.split.rpartition) |
18 changes: 18 additions & 0 deletions
18
tests/functional/c/consider/consider_using_maxsplit_arg.txt
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,18 @@ | ||
consider-using-maxsplit-arg:5:12::Consider using '1,2,3'.split(sep=',',maxsplit=1)[0] instead | ||
consider-using-maxsplit-arg:6:11::"Consider using '1,2,3'[::-1].split(sep=',',maxsplit=1)[0] instead" | ||
consider-using-maxsplit-arg:9:12::Consider using SEQ.split(sep=',',maxsplit=1)[0] instead | ||
consider-using-maxsplit-arg:10:11::Consider using SEQ.rsplit(sep=',',maxsplit=1)[-1] instead | ||
consider-using-maxsplit-arg:11:12::Consider using SEQ.split(sep=',',maxsplit=1)[0] instead | ||
consider-using-maxsplit-arg:12:11::Consider using SEQ.rsplit(sep=',',maxsplit=1)[-1] instead | ||
consider-using-maxsplit-arg:44:12::Consider using Foo.class_str.split(sep=',',maxsplit=1)[0] instead | ||
consider-using-maxsplit-arg:45:11::Consider using Foo.class_str.rsplit(sep=',',maxsplit=1)[-1] instead | ||
consider-using-maxsplit-arg:46:12::Consider using Foo.class_str.split(sep=',',maxsplit=1)[0] instead | ||
consider-using-maxsplit-arg:47:11::Consider using Foo.class_str.rsplit(sep=',',maxsplit=1)[-1] instead | ||
consider-using-maxsplit-arg:55:12::Consider using bar.get_string().split(sep=',',maxsplit=1)[0] instead | ||
consider-using-maxsplit-arg:56:11::Consider using bar.get_string().rsplit(sep=',',maxsplit=1)[-1] instead | ||
consider-using-maxsplit-arg:65:10::Consider using s.split(sep=' ',maxsplit=1)[0] instead | ||
consider-using-maxsplit-arg:66:10::Consider using s.rsplit(sep=' ',maxsplit=1)[-1] instead | ||
consider-using-maxsplit-arg:74:6::Consider using Bar.split.split(sep=',',maxsplit=1)[0] instead | ||
consider-using-maxsplit-arg:75:6::Consider using Bar.split.rsplit(sep=',',maxsplit=1)[-1] instead | ||
consider-using-maxsplit-arg:76:6::Consider using Bar.split.split(sep=',',maxsplit=1)[0] instead | ||
consider-using-maxsplit-arg:77:6::Consider using Bar.split.rsplit(sep=',',maxsplit=1)[-1] instead |
81 changes: 0 additions & 81 deletions
81
tests/functional/c/consider/consider_using_str_partition.py
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
tests/functional/c/consider/consider_using_str_partition.txt
This file was deleted.
Oops, something went wrong.
Did you mean rsplit instead of rpartition on the second example ?