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

Add an exception for IndexError inside uninferable_final_decorator #6532

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog
Expand Up @@ -307,6 +307,9 @@ What's New in Pylint 2.13.9?
============================
Release date: TBA

* Fix ``IndexError`` crash in ``uninferable_final_decorators`` method.

Relates to #6531


What's New in Pylint 2.13.8?
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.13.rst
Expand Up @@ -639,3 +639,7 @@ Other Changes
``open``

Closes #6414

* Fix ``IndexError`` crash in ``uninferable_final_decorators`` method.

Relates to #6531
5 changes: 4 additions & 1 deletion pylint/checkers/utils.py
Expand Up @@ -849,9 +849,12 @@ def uninferable_final_decorators(
for decorator in getattr(node, "nodes", []):
if isinstance(decorator, nodes.Attribute):
try:
import_node = decorator.expr.lookup(decorator.expr.name)[1][0]
_, import_nodes = decorator.expr.lookup(decorator.expr.name)
except AttributeError:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the failed test this should be fixable with an isinstance on decorator.expr. I think we assume expr to a LocalsDictNodeNG while apparently in those regression tests it is a nodes.Attribute. Should we try and fix that while we're at it?
In general I prefer explicitness over broader try...excepts.

Copy link
Member Author

@mbyrnepr2 mbyrnepr2 May 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decorator.expr has no attribute loookup (it is an instance of nodes.Attribute). I could try adding a hasattr instead of an isinstance if I understood you.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But normally it does right? My hunch is that this code works well for @decorator.func() but not for @decorator.module.func(). Apparently sometimes expr sometimes does have lookup so instead of using hasattr can't we reliable determine what instance expr has and whether that instance is supposed to have lookup?

continue
if not import_nodes:
mbyrnepr2 marked this conversation as resolved.
Show resolved Hide resolved
continue
import_node = import_nodes[0]
elif isinstance(decorator, nodes.Name):
lookup_values = decorator.lookup(decorator.name)
if lookup_values[1]:
Expand Down
28 changes: 28 additions & 0 deletions tests/functional/r/regression/regression_6531_crash_index_error.py
@@ -0,0 +1,28 @@
# pylint: disable=missing-docstring, redefined-outer-name
mbyrnepr2 marked this conversation as resolved.
Show resolved Hide resolved

import pytest


class Wallet:
def __init__(self):
self.balance = 0

def add_cash(self, earned):
self.balance += earned

def spend_cash(self, spent):
self.balance -= spent

@pytest.fixture
def my_wallet():
'''Returns a Wallet instance with a zero balance'''
return Wallet()

@pytest.mark.parametrize("earned,spent,expected", [
(30, 10, 20),
(20, 2, 18),
])
def test_transactions(my_wallet, earned, spent, expected):
my_wallet.add_cash(earned)
my_wallet.spend_cash(spent)
assert my_wallet.balance == expected