Skip to content

Commit

Permalink
Fix crash when an attribute node was used inside an unary op (#8209) (#…
Browse files Browse the repository at this point in the history
…8225)

Closes #8207

(cherry picked from commit 70f7e3a)

Co-authored-by: Pierre Sassoulas <[email protected]>
  • Loading branch information
github-actions[bot] and Pierre-Sassoulas authored Feb 7, 2023
1 parent 3c27c47 commit 55f1482
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8207.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash happening when a class attribute was negated in the start argument of an enumerate.

Closes #8207
7 changes: 5 additions & 2 deletions pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2338,13 +2338,16 @@ def _enumerate_with_start(
return False, confidence

def _get_start_value(self, node: nodes.NodeNG) -> tuple[int | None, Confidence]:
if isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute)):
if (
isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute))
or isinstance(node, nodes.UnaryOp)
and isinstance(node.operand, nodes.Attribute)
):
inferred = utils.safe_infer(node)
start_val = inferred.value if inferred else None
return start_val, INFERENCE
if isinstance(node, nodes.UnaryOp):
return node.operand.value, HIGH
if isinstance(node, nodes.Const):
return node.value, HIGH

return None, HIGH
14 changes: 14 additions & 0 deletions tests/functional/r/regression_02/regression_8207.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Regression test for 8207."""

# pylint: disable=missing-docstring,too-few-public-methods

class Example:
def __init__(self):
self.offset = -10

def minus_offset(self):
return {
(x, x): value
for x, row in enumerate([(5, 10), (20, 30)])
for y, value in enumerate(row, -self.offset)
}

0 comments on commit 55f1482

Please sign in to comment.