Skip to content

Commit ab3e0f1

Browse files
committed
Fix crash in refactoring checker from unaryop with variable
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 pylint-dev#9074
1 parent b8a7cc5 commit ab3e0f1

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

doc/whatsnew/fragments/9074.bugfix

+3
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,7 @@ def _get_start_value(self, node: nodes.NodeNG) -> tuple[int | None, Confidence]:
23622362
if (
23632363
isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute))
23642364
or isinstance(node, nodes.UnaryOp)
2365-
and isinstance(node.operand, nodes.Attribute)
2365+
and isinstance(node.operand, (nodes.Attribute, nodes.Name))
23662366
):
23672367
inferred = utils.safe_infer(node)
23682368
start_val = inferred.value if inferred else None
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)