Skip to content

Commit 6e6652e

Browse files
committed
handle Attribute
1 parent 694d302 commit 6e6652e

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

doc/whatsnew/fragments/7821.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixes bug that resulted in crash during ``unnecessary_list_index_lookup`` check.
2+
3+
Closes #7821

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,15 +2301,13 @@ def _enumerate_with_start(
23012301
return False, confidence
23022302

23032303
def _get_start_value(self, node: nodes.NodeNG) -> tuple[int | None, Confidence]:
2304-
confidence = HIGH
2305-
2306-
if isinstance(node, (nodes.Name, nodes.Call)):
2304+
if isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute)):
23072305
inferred = utils.safe_infer(node)
23082306
start_val = inferred.value if inferred else None
2309-
confidence = INFERENCE
2310-
elif isinstance(node, nodes.UnaryOp):
2311-
start_val = node.operand.value
2312-
else:
2313-
start_val = node.value
2307+
return start_val, INFERENCE
2308+
if isinstance(node, nodes.UnaryOp):
2309+
return node.operand.value, HIGH
2310+
if isinstance(node, nodes.Const):
2311+
return node.value, HIGH
23142312

2315-
return start_val, confidence
2313+
return None, HIGH

tests/functional/u/unnecessary/unnecessary_list_index_lookup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,9 @@ 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

0 commit comments

Comments
 (0)