Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when using enumerate with start and a class attribute #7824

Merged
merged 4 commits into from Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/7821.bugfix
@@ -0,0 +1,3 @@
Fixes bug that resulted in crash during ``unnecessary_list_index_lookup`` check.
Pierre-Sassoulas marked this conversation as resolved.
Show resolved Hide resolved

Closes #7821
16 changes: 7 additions & 9 deletions pylint/checkers/refactoring/refactoring_checker.py
Expand Up @@ -2306,15 +2306,13 @@ def _enumerate_with_start(
return False, confidence

def _get_start_value(self, node: nodes.NodeNG) -> tuple[int | None, Confidence]:
confidence = HIGH

if isinstance(node, (nodes.Name, nodes.Call)):
if isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute)):
inferred = utils.safe_infer(node)
start_val = inferred.value if inferred else None
confidence = INFERENCE
elif isinstance(node, nodes.UnaryOp):
start_val = node.operand.value
else:
start_val = node.value
return start_val, INFERENCE
if isinstance(node, nodes.UnaryOp):
return node.operand.value, HIGH
if isinstance(node, nodes.Const):
return node.value, HIGH

return start_val, confidence
return None, HIGH
11 changes: 11 additions & 0 deletions tests/functional/u/unnecessary/unnecessary_list_index_lookup.py
Expand Up @@ -138,3 +138,14 @@ def return_start(start):

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

class Command:
def _get_extra_attrs(self, extra_columns):
self.extra_rows_start = 8 # pylint: disable=attribute-defined-outside-init
for index, column in enumerate(extra_columns, start=self.extra_rows_start):
pass

Y_START = 2
nums = list(range(20))
for y, x in enumerate(nums, start=Y_START + 1):
pass