summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Byrne <31762852+mbyrnepr2@users.noreply.github.com>2022-05-07 21:23:26 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2022-05-13 17:30:49 +0200
commit18cb0264b1c387b7c90543162e1ebb8b839af6b4 (patch)
tree6b3cf9b077777e605f859dd536bbfeb88001b44f
parent45cbae2bab9001bb2f159103490a02d63e75ee5b (diff)
downloadpylint-git-18cb0264b1c387b7c90543162e1ebb8b839af6b4.tar.gz
Add an exception for `IndexError` inside `uninferable_final_decorator` (#6532)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com> Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>
-rw-r--r--ChangeLog3
-rw-r--r--doc/whatsnew/2.13.rst4
-rw-r--r--pylint/checkers/utils.py30
-rw-r--r--tests/functional/r/regression/regression_6531_crash_index_error.py30
4 files changed, 55 insertions, 12 deletions
diff --git a/ChangeLog b/ChangeLog
index 7d8d60e85..07bf60acb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -20,6 +20,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?
diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst
index 56914559f..e851af65d 100644
--- a/doc/whatsnew/2.13.rst
+++ b/doc/whatsnew/2.13.rst
@@ -639,3 +639,7 @@ Other Changes
``open``
Closes #6414
+
+* Fix ``IndexError`` crash in ``uninferable_final_decorators`` method.
+
+ Relates to #6531
diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py
index ec5f2cddc..9cba6e057 100644
--- a/pylint/checkers/utils.py
+++ b/pylint/checkers/utils.py
@@ -820,28 +820,34 @@ def uninferable_final_decorators(
"""
decorators = []
for decorator in getattr(node, "nodes", []):
+ import_nodes: tuple[nodes.Import | nodes.ImportFrom] | None = None
+
+ # Get the `Import` node. The decorator is of the form: @module.name
if isinstance(decorator, nodes.Attribute):
- try:
- import_node = decorator.expr.lookup(decorator.expr.name)[1][0]
- except AttributeError:
- continue
+ inferred = safe_infer(decorator.expr)
+ if isinstance(inferred, nodes.Module) and inferred.qname() == "typing":
+ _, import_nodes = decorator.expr.lookup(decorator.expr.name)
+
+ # Get the `ImportFrom` node. The decorator is of the form: @name
elif isinstance(decorator, nodes.Name):
- lookup_values = decorator.lookup(decorator.name)
- if lookup_values[1]:
- import_node = lookup_values[1][0]
- else:
- continue # pragma: no cover # Covered on Python < 3.8
- else:
+ _, import_nodes = decorator.lookup(decorator.name)
+
+ # The `final` decorator is expected to be found in the
+ # import_nodes. Continue if we don't find any `Import` or `ImportFrom`
+ # nodes for this decorator.
+ if not import_nodes:
continue
+ import_node = import_nodes[0]
if not isinstance(import_node, (astroid.Import, astroid.ImportFrom)):
continue
import_names = dict(import_node.names)
- # from typing import final
+ # Check if the import is of the form: `from typing import final`
is_from_import = ("final" in import_names) and import_node.modname == "typing"
- # import typing
+
+ # Check if the import is of the form: `import typing`
is_import = ("typing" in import_names) and getattr(
decorator, "attrname", None
) == "final"
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 000000000..6cdc96617
--- /dev/null
+++ b/tests/functional/r/regression/regression_6531_crash_index_error.py
@@ -0,0 +1,30 @@
+"""Regression test for https://github.com/PyCQA/pylint/issues/6531."""
+
+# 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