Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Oct 27, 2022
1 parent ddc9fa3 commit ac4d86a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7682.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``unnecessary-list-index-lookup`` will not be emitted if ``enumerate`` is called with a `start` arg or kwarg.

Closes #7682
14 changes: 13 additions & 1 deletion pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2108,7 +2108,7 @@ def _check_unnecessary_list_index_lookup(
# destructured, so we can't necessarily use it.
return

if node.iter.keywords or len(node.iter.args) > 1:
if self._enumerate_with_start(node):
# enumerate is being called with start arg/kwarg so resulting index lookup
# is not redundant so we should not report an error.
return
Expand Down Expand Up @@ -2204,3 +2204,15 @@ def _check_unnecessary_list_index_lookup(
args=(node.target.elts[1].name,),
confidence=HIGH,
)

def _enumerate_with_start(self, node: nodes.For | nodes.Comprehension) -> bool:
"""Check presence of `start` kwarg or second argument to enumerate
For example
`enumerate([1,2,3], start=1)`
`enumerate([1,2,3], 1)`
"""
if len(node.iter.args) > 1:
# We assume the second argument to `enumerate` is the `start` arg.
return True

return any(keyword.arg == "start" for keyword in node.iter.keywords)

0 comments on commit ac4d86a

Please sign in to comment.