|
| 1 | +SEQ = "1,2,3" |
| 2 | + |
| 3 | +class Foo(str): |
| 4 | + class_str = "1,2,3" |
| 5 | + |
| 6 | + def split(self, sep=None, maxsplit=-1) -> list[str]: |
| 7 | + return super().split(sep, maxsplit) |
| 8 | + |
| 9 | +class Bar(): |
| 10 | + split = "1,2,3" |
| 11 | + |
| 12 | +# Errors |
| 13 | +## Test split called directly on string literal |
| 14 | +"1,2,3".split(",")[0] # [missing-maxsplit-arg] |
| 15 | +"1,2,3".split(",")[-1] # [missing-maxsplit-arg] |
| 16 | +"1,2,3".rsplit(",")[0] # [missing-maxsplit-arg] |
| 17 | +"1,2,3".rsplit(",")[-1] # [missing-maxsplit-arg] |
| 18 | + |
| 19 | +## Test split called on string variable |
| 20 | +SEQ.split(",")[0] # [missing-maxsplit-arg] |
| 21 | +SEQ.split(",")[-1] # [missing-maxsplit-arg] |
| 22 | +SEQ.rsplit(",")[0] # [missing-maxsplit-arg] |
| 23 | +SEQ.rsplit(",")[-1] # [missing-maxsplit-arg] |
| 24 | + |
| 25 | +## Test split called on class attribute |
| 26 | +Foo.class_str.split(",")[0] # [missing-maxsplit-arg] |
| 27 | +Foo.class_str.split(",")[-1] # [missing-maxsplit-arg] |
| 28 | +Foo.class_str.rsplit(",")[0] # [missing-maxsplit-arg] |
| 29 | +Foo.class_str.rsplit(",")[-1] # [missing-maxsplit-arg] |
| 30 | + |
| 31 | +## Test split called on sliced string |
| 32 | +"1,2,3"[::-1].split(",")[0] # [missing-maxsplit-arg] |
| 33 | +"1,2,3"[::-1][::-1].split(",")[0] # [missing-maxsplit-arg] |
| 34 | +SEQ[:3].split(",")[0] # [missing-maxsplit-arg] |
| 35 | +Foo.class_str[1:3].split(",")[-1] # [missing-maxsplit-arg] |
| 36 | +"1,2,3"[::-1].rsplit(",")[0] # [missing-maxsplit-arg] |
| 37 | +SEQ[:3].rsplit(",")[0] # [missing-maxsplit-arg] |
| 38 | +Foo.class_str[1:3].rsplit(",")[-1] # [missing-maxsplit-arg] |
| 39 | + |
| 40 | +## Test sep given as named argument |
| 41 | +"1,2,3".split(sep=",")[0] # [missing-maxsplit-arg] |
| 42 | +"1,2,3".split(sep=",")[-1] # [missing-maxsplit-arg] |
| 43 | +"1,2,3".rsplit(sep=",")[0] # [missing-maxsplit-arg] |
| 44 | +"1,2,3".rsplit(sep=",")[-1] # [missing-maxsplit-arg] |
| 45 | + |
| 46 | +## Special cases |
| 47 | +"1,2,3".split("\n")[0] # [missing-maxsplit-arg] |
| 48 | +"1,2,3".split("split")[-1] # [missing-maxsplit-arg] |
| 49 | +"1,2,3".rsplit("rsplit")[0] # [missing-maxsplit-arg] |
| 50 | + |
| 51 | +## Test class attribute named split |
| 52 | +Bar.split.split(",")[0] # [missing-maxsplit-arg] |
| 53 | +Bar.split.split(",")[-1] # [missing-maxsplit-arg] |
| 54 | +Bar.split.rsplit(",")[0] # [missing-maxsplit-arg] |
| 55 | +Bar.split.rsplit(",")[-1] # [missing-maxsplit-arg] |
| 56 | + |
| 57 | +## Test unpacked dict literal kwargs |
| 58 | +"1,2,3".split(**{"sep": ","})[0] # [missing-maxsplit-arg] |
| 59 | + |
| 60 | + |
| 61 | +# OK |
| 62 | +## Test not accessing the first or last element |
| 63 | +### Test split called directly on string literal |
| 64 | +"1,2,3".split(",")[1] |
| 65 | +"1,2,3".split(",")[-2] |
| 66 | +"1,2,3".rsplit(",")[1] |
| 67 | +"1,2,3".rsplit(",")[-2] |
| 68 | + |
| 69 | +### Test split called on string variable |
| 70 | +SEQ.split(",")[1] |
| 71 | +SEQ.split(",")[-2] |
| 72 | +SEQ.rsplit(",")[1] |
| 73 | +SEQ.rsplit(",")[-2] |
| 74 | + |
| 75 | +### Test split called on class attribute |
| 76 | +Foo.class_str.split(",")[1] |
| 77 | +Foo.class_str.split(",")[-2] |
| 78 | +Foo.class_str.rsplit(",")[1] |
| 79 | +Foo.class_str.rsplit(",")[-2] |
| 80 | + |
| 81 | +### Test split called on sliced string |
| 82 | +"1,2,3"[::-1].split(",")[1] |
| 83 | +SEQ[:3].split(",")[1] |
| 84 | +Foo.class_str[1:3].split(",")[-2] |
| 85 | +"1,2,3"[::-1].rsplit(",")[1] |
| 86 | +SEQ[:3].rsplit(",")[1] |
| 87 | +Foo.class_str[1:3].rsplit(",")[-2] |
| 88 | + |
| 89 | +### Test sep given as named argument |
| 90 | +"1,2,3".split(sep=",")[1] |
| 91 | +"1,2,3".split(sep=",")[-2] |
| 92 | +"1,2,3".rsplit(sep=",")[1] |
| 93 | +"1,2,3".rsplit(sep=",")[-2] |
| 94 | + |
| 95 | +## Test varying maxsplit argument |
| 96 | +### str.split() tests |
| 97 | +"1,2,3".split(sep=",", maxsplit=1)[-1] |
| 98 | +"1,2,3".split(sep=",", maxsplit=1)[0] |
| 99 | +"1,2,3".split(sep=",", maxsplit=2)[-1] |
| 100 | +"1,2,3".split(sep=",", maxsplit=2)[0] |
| 101 | +"1,2,3".split(sep=",", maxsplit=2)[1] |
| 102 | + |
| 103 | +### str.rsplit() tests |
| 104 | +"1,2,3".rsplit(sep=",", maxsplit=1)[-1] |
| 105 | +"1,2,3".rsplit(sep=",", maxsplit=1)[0] |
| 106 | +"1,2,3".rsplit(sep=",", maxsplit=2)[-1] |
| 107 | +"1,2,3".rsplit(sep=",", maxsplit=2)[0] |
| 108 | +"1,2,3".rsplit(sep=",", maxsplit=2)[1] |
| 109 | + |
| 110 | +## Test user-defined split |
| 111 | +Foo("1,2,3").split(",")[0] |
| 112 | +Foo("1,2,3").split(",")[-1] |
| 113 | +Foo("1,2,3").rsplit(",")[0] |
| 114 | +Foo("1,2,3").rsplit(",")[-1] |
| 115 | + |
| 116 | +## Test split called on sliced list |
| 117 | +["1", "2", "3"][::-1].split(",")[0] |
| 118 | + |
| 119 | +## Test class attribute named split |
| 120 | +Bar.split[0] |
| 121 | +Bar.split[-1] |
| 122 | +Bar.split[0] |
| 123 | +Bar.split[-1] |
| 124 | + |
| 125 | +## Test unpacked dict literal kwargs |
| 126 | +"1,2,3".split(",", **{"maxsplit": 1})[0] |
| 127 | +"1,2,3".split(**{"sep": ",", "maxsplit": 1})[0] |
| 128 | + |
| 129 | + |
| 130 | +# TODO |
| 131 | + |
| 132 | +## Test variable split result index |
| 133 | +## TODO: These require the ability to resolve a variable name to a value |
| 134 | +# Errors |
| 135 | +result_index = 0 |
| 136 | +"1,2,3".split(",")[result_index] # TODO: [missing-maxsplit-arg] |
| 137 | +result_index = -1 |
| 138 | +"1,2,3".split(",")[result_index] # TODO: [missing-maxsplit-arg] |
| 139 | +# OK |
| 140 | +result_index = 1 |
| 141 | +"1,2,3".split(",")[result_index] |
| 142 | +result_index = -2 |
| 143 | +"1,2,3".split(",")[result_index] |
| 144 | + |
| 145 | + |
| 146 | +## Test split result index modified in loop |
| 147 | +## TODO: These require the ability to recognize being in a loop where: |
| 148 | +## - the result of split called on a string is indexed by a variable |
| 149 | +## - the variable index above is modified |
| 150 | +# OK |
| 151 | +result_index = 0 |
| 152 | +for j in range(3): |
| 153 | + print(SEQ.split(",")[result_index]) |
| 154 | + result_index = result_index + 1 |
| 155 | + |
| 156 | + |
| 157 | +## Test accessor |
| 158 | +## TODO: These require the ability to get the return type of a method |
| 159 | +## (possibly via `typing::is_string`) |
| 160 | +class Baz(): |
| 161 | + def __init__(self): |
| 162 | + self.my_str = "1,2,3" |
| 163 | + |
| 164 | + def get_string(self) -> str: |
| 165 | + return self.my_str |
| 166 | + |
| 167 | +# Errors |
| 168 | +Baz().get_string().split(",")[0] # TODO: [missing-maxsplit-arg] |
| 169 | +Baz().get_string().split(",")[-1] # TODO: [missing-maxsplit-arg] |
| 170 | +# OK |
| 171 | +Baz().get_string().split(",")[1] |
| 172 | +Baz().get_string().split(",")[-2] |
| 173 | + |
| 174 | + |
| 175 | +## Test unpacked dict instance kwargs |
| 176 | +## TODO: These require the ability to resolve a dict variable name to a value |
| 177 | +# Errors |
| 178 | +kwargs_without_maxsplit = {"seq": ","} |
| 179 | +"1,2,3".split(**kwargs_without_maxsplit)[0] # TODO: [missing-maxsplit-arg] |
| 180 | +# OK |
| 181 | +kwargs_with_maxsplit = {"maxsplit": 1} |
| 182 | +"1,2,3".split(",", **kwargs_with_maxsplit)[0] # TODO: false positive |
| 183 | +kwargs_with_maxsplit = {"sep": ",", "maxsplit": 1} |
| 184 | +"1,2,3".split(**kwargs_with_maxsplit)[0] # TODO: false positive |
0 commit comments