Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when an attribute node was used inside an unary op #8209

Merged
merged 2 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}