Skip to content

Commit 6abd0a0

Browse files
Fix crash when using enumerate with start and a class attribute (#7824)
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent a9c1cda commit 6abd0a0

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

doc/whatsnew/fragments/7821.bugfix

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixes a crash in the ``unnecessary_list_index_lookup`` check when using ``enumerate`` with ``start`` and a class attribute.
2+
3+
Closes #7821

pylint/checkers/refactoring/refactoring_checker.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -2306,15 +2306,13 @@ def _enumerate_with_start(
23062306
return False, confidence
23072307

23082308
def _get_start_value(self, node: nodes.NodeNG) -> tuple[int | None, Confidence]:
2309-
confidence = HIGH
2310-
2311-
if isinstance(node, (nodes.Name, nodes.Call)):
2309+
if isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute)):
23122310
inferred = utils.safe_infer(node)
23132311
start_val = inferred.value if inferred else None
2314-
confidence = INFERENCE
2315-
elif isinstance(node, nodes.UnaryOp):
2316-
start_val = node.operand.value
2317-
else:
2318-
start_val = node.value
2312+
return start_val, INFERENCE
2313+
if isinstance(node, nodes.UnaryOp):
2314+
return node.operand.value, HIGH
2315+
if isinstance(node, nodes.Const):
2316+
return node.value, HIGH
23192317

2320-
return start_val, confidence
2318+
return None, HIGH

tests/functional/u/unnecessary/unnecessary_list_index_lookup.py

+11
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,14 @@ def return_start(start):
138138

139139
for idx, val in enumerate():
140140
print(my_list[idx])
141+
142+
class Command:
143+
def _get_extra_attrs(self, extra_columns):
144+
self.extra_rows_start = 8 # pylint: disable=attribute-defined-outside-init
145+
for index, column in enumerate(extra_columns, start=self.extra_rows_start):
146+
pass
147+
148+
Y_START = 2
149+
nums = list(range(20))
150+
for y, x in enumerate(nums, start=Y_START + 1):
151+
pass

0 commit comments

Comments
 (0)