From e7053b7846d5f1fc0391e8ba75e92fb3cc0ef0ed Mon Sep 17 00:00:00 2001 From: Mark Byrne Date: Sat, 7 May 2022 00:26:38 +0200 Subject: [PATCH] Add an exception for `IndexError` inside `uninferable_final_decorators` method. --- ChangeLog | 2 ++ doc/whatsnew/2.14.rst | 2 ++ pylint/checkers/utils.py | 2 +- .../regression_6531_crash_index_error.py | 28 +++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/functional/r/regression/regression_6531_crash_index_error.py diff --git a/ChangeLog b/ChangeLog index aec24186b6..c5983fd3a3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -25,6 +25,8 @@ Release date: TBA Closes #4301 +* Fix ``IndexError`` crash in ``uninferable_final_decorators`` method. + * By default the similarity checker will now ignore imports and ignore function signatures when computing duplication. If you want to keep the previous behaviour set ``ignore-imports`` and ``ignore-signatures`` to ``False``. diff --git a/doc/whatsnew/2.14.rst b/doc/whatsnew/2.14.rst index d59c4792ec..cfb4b57072 100644 --- a/doc/whatsnew/2.14.rst +++ b/doc/whatsnew/2.14.rst @@ -305,6 +305,8 @@ Other Changes * By default the similarity checker will now ignore imports and ignore function signatures when computing duplication. If you want to keep the previous behaviour set ``ignore-imports`` and ``ignore-signatures`` to ``False``. +* Fix ``IndexError`` crash in ``uninferable_final_decorators`` method. + * Pylint now expands the user path (i.e. ``~`` to ``home/yusef/``) and expands environment variables (i.e. ``home/$USER/$project`` to ``home/yusef/pylint`` for ``USER=yusef`` and ``project=pylint``) for pyreverse's ``output-directory``, ``import-graph``, ``ext-import-graph``, ``int-import-graph`` options, and the spell checker's ``spelling-private-dict-file`` diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py index 5a48ecf0a3..ecb7b68252 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -850,7 +850,7 @@ def uninferable_final_decorators( if isinstance(decorator, nodes.Attribute): try: import_node = decorator.expr.lookup(decorator.expr.name)[1][0] - except AttributeError: + except (AttributeError, IndexError): continue elif isinstance(decorator, nodes.Name): lookup_values = decorator.lookup(decorator.name) diff --git a/tests/functional/r/regression/regression_6531_crash_index_error.py b/tests/functional/r/regression/regression_6531_crash_index_error.py new file mode 100644 index 0000000000..9a2de9cdb4 --- /dev/null +++ b/tests/functional/r/regression/regression_6531_crash_index_error.py @@ -0,0 +1,28 @@ +# pylint: disable=missing-docstring, redefined-outer-name + +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