flake8-simplify
flake8-simplify copied to clipboard
SIM113: Use enumerate
Explanation
Use 'enumerate' to get a running number over an iterable.
Example
# Bad
idx = 0
for el in iterable:
...
idx += 1
# Good
for idx, el in enumerate(iterable):
...
This is a false-positive:
for x in xs:
cm[x] += 1
This is another false possitive
results = []
next_ = 0
for i in range(10):
if i % 2 == 0:
continue
results[next_] = i
next_ += 1
@pablojadz Could you please run pip install flake8-simplify --upgrade
and test again? You should have flake8-simplify==0.14.1
and this issue should not exist anymore.
I believe I have another false-positive:
pos = len(output) - token[1]
for __ in range(token[0]):
output += output[pos]
pos += 1
This code appends token[0]
characters from position pos
to the end of the string output
. (This is a common pattern in LZ-style decompressors.)
Another possible issues
for idx, el in enumerate([]): pass
print(idx) --> NameError
I would propose to silence the error, when the loop variable is used after the loop