Skip to content

Commit

Permalink
account for calling start with 0 or negative num
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Oct 28, 2022
1 parent 29d9200 commit d6d72e2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion doc/whatsnew/fragments/7682.bugfix
@@ -1,3 +1,3 @@
``unnecessary-list-index-lookup`` will not be emitted if ``enumerate`` is called with a `start` arg or kwarg.
``unnecessary-list-index-lookup`` will not be wrongly emitted if ``enumerate`` is called with ``start``.

Closes #7682
34 changes: 29 additions & 5 deletions pylint/checkers/refactoring/refactoring_checker.py
Expand Up @@ -2206,13 +2206,37 @@ def _check_unnecessary_list_index_lookup(
)

def _enumerate_with_start(self, node: nodes.For | nodes.Comprehension) -> bool:
"""Check presence of `start` kwarg or second argument to enumerate
For example
"""Check presence of `start` kwarg or second argument to enumerate.
For example:
`enumerate([1,2,3], start=1)`
`enumerate([1,2,3], 1)`
If `start` is assigned to `0`, the default value, this is equivalent to
not calling `enumerate` with start.
"""
if len(node.iter.args) > 1:
# We assume the second argument to `enumerate` is the `start` arg.
return True
# We assume the second argument to `enumerate` is the `start` int arg.
# It's a reasonable assumption for now as it's the only possible argument:
# https://docs.python.org/3/library/functions.html#enumerate
start_arg = node.iter.args[1]

start_val = (
start_arg.operand.value
if isinstance(start_arg, nodes.UnaryOp)
else start_arg.value
)
return not start_val == 0

for keyword in node.iter.keywords:
if keyword.arg == "start":
start_val = (
keyword.value.operand.value
if isinstance(keyword.value, nodes.UnaryOp)
else keyword.value.value
)

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

return False
17 changes: 16 additions & 1 deletion tests/functional/u/unnecessary/unnecessary_list_index_lookup.py
Expand Up @@ -97,4 +97,19 @@ def process_list_again(data):
]

for idx, val in enumerate(series, start=2):
print(my_list[idx])
print(series[idx])

for idx, val in enumerate(series, 2):
print(series[idx])

for idx, val in enumerate(series, start=-2):
print(series[idx])

for idx, val in enumerate(series, -2):
print(series[idx])

for idx, val in enumerate(series, start=0):
print(series[idx]) # [unnecessary-list-index-lookup]

for idx, val in enumerate(series, 0):
print(series[idx]) # [unnecessary-list-index-lookup]
Expand Up @@ -2,3 +2,5 @@ unnecessary-list-index-lookup:8:10:8:22::Unnecessary list index lookup, use 'val
unnecessary-list-index-lookup:43:52:43:64::Unnecessary list index lookup, use 'val' instead:HIGH
unnecessary-list-index-lookup:46:10:46:22::Unnecessary list index lookup, use 'val' instead:HIGH
unnecessary-list-index-lookup:74:10:74:27::Unnecessary list index lookup, use 'val' instead:HIGH
unnecessary-list-index-lookup:112:10:112:21::Unnecessary list index lookup, use 'val' instead:HIGH
unnecessary-list-index-lookup:115:10:115:21::Unnecessary list index lookup, use 'val' instead:HIGH

0 comments on commit d6d72e2

Please sign in to comment.