Skip to content

Commit aa29201

Browse files
authored
Fix crash in refactoring checker from unaryop with variable (#9075)
Fixes: ``` File "python3.10/site-packages/pylint/utils/ast_walker.py", line 91, in walk callback(astroid) File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 700, in visit_for self._check_unnecessary_list_index_lookup(node) File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2227, in _check_unnecessary_list_index_lookup has_start_arg, confidence = self._enumerate_with_start(node) File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2352, in _enumerate_with_start start_val, confidence = self._get_start_value(keyword.value) File "python3.10/site-packages/pylint/checkers/refactoring/refactoring_checker.py", line 2369, in _get_start_value return node.operand.value, HIGH AttributeError: 'Name' object has no attribute 'value' ``` Crash is reproducible if you have something like this: ```python x=5 for _ in enumerate(range, start=-x): ... ``` As a workaround, remove the unary op before `for` loop (i.e. change the variable used). Closes #9074 Co-authored-by: Hashem Nasarat <[email protected]>
1 parent a4aff97 commit aa29201

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

doc/whatsnew/fragments/9074.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix crash in refactoring checker when unary operand used with variable in for loop.
2+
3+
Closes #9074

pylint/checkers/refactoring/refactoring_checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2356,7 +2356,7 @@ def _get_start_value(self, node: nodes.NodeNG) -> tuple[int | None, Confidence]:
23562356
if (
23572357
isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute))
23582358
or isinstance(node, nodes.UnaryOp)
2359-
and isinstance(node.operand, nodes.Attribute)
2359+
and isinstance(node.operand, (nodes.Attribute, nodes.Name))
23602360
):
23612361
inferred = utils.safe_infer(node)
23622362
start_val = inferred.value if inferred else None
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""Regression test."""
2+
def crash_on_unary_op_with_name():
3+
"""Should not crash with -idx."""
4+
mylist = []
5+
idx = 5
6+
for _i, _val in enumerate(mylist, start=-idx):
7+
pass

tests/functional/r/regression/regression_9074_refactor_loop_with_unary_variable.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)