Skip to content

Commit

Permalink
test for start as a var
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Oct 31, 2022
1 parent d6d72e2 commit 2a97187
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
28 changes: 16 additions & 12 deletions pylint/checkers/refactoring/refactoring_checker.py
Expand Up @@ -2221,22 +2221,26 @@ def _enumerate_with_start(self, node: nodes.For | nodes.Comprehension) -> bool:
# 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
)
start_val = self._get_start_value(start_arg)
if start_val is None:
return False
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
)

start_val = self._get_start_value(keyword.value)
if start_val is None:
return False
return not start_val == 0

return False

def _get_start_value(self, node: nodes.NodeNG) -> Any:
if isinstance(node, nodes.Name):
inferred = utils.safe_infer(node)
start_val = inferred.value if inferred else None
elif isinstance(node, nodes.UnaryOp):
start_val = node.operand.value
else:
start_val = node.value
return start_val
Expand Up @@ -113,3 +113,10 @@ def process_list_again(data):

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

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

for idx, val in enumerate(series, START):
print(series[idx]) # [unnecessary-list-index-lookup]
Expand Up @@ -4,3 +4,5 @@ unnecessary-list-index-lookup:46:10:46:22::Unnecessary list index lookup, use 'v
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
unnecessary-list-index-lookup:119:10:119:21::Unnecessary list index lookup, use 'val' instead:HIGH
unnecessary-list-index-lookup:122:10:122:21::Unnecessary list index lookup, use 'val' instead:HIGH

0 comments on commit 2a97187

Please sign in to comment.