From 2e383f3da69c71925904b15ad2f97e247cda419f Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sat, 30 Apr 2022 12:21:05 -0400 Subject: Fix a crash involving two starred expressions inside a call (#1535) --- ChangeLog | 5 +++++ astroid/protocols.py | 3 +++ tests/unittest_protocols.py | 11 +++++++++++ 3 files changed, 19 insertions(+) diff --git a/ChangeLog b/ChangeLog index 2dd63c70..c69642f1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -14,6 +14,11 @@ Release date: TBA * Fix ``col_offset`` attribute for nodes involving ``with`` on ``PyPy``. +* Fixed a crash involving two starred expressions: one inside a comprehension, + both inside a call. + + Refs PyCQA/pylint#6372 + * Made ``FunctionDef.implicit_parameters`` return 1 for methods by making ``FunctionDef.is_bound`` return ``True``, as it does for class methods. diff --git a/astroid/protocols.py b/astroid/protocols.py index 1ec1121b..f1fcec0d 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -690,6 +690,9 @@ def starred_assigned_stmts( if isinstance(stmt, nodes.Assign): value = stmt.value lhs = stmt.targets[0] + if not isinstance(lhs, nodes.BaseContainer): + yield util.Uninferable + return if sum(1 for _ in lhs.nodes_of_class(nodes.Starred)) > 1: raise InferenceError( diff --git a/tests/unittest_protocols.py b/tests/unittest_protocols.py index 7c3abb8d..59f82c39 100644 --- a/tests/unittest_protocols.py +++ b/tests/unittest_protocols.py @@ -125,6 +125,17 @@ class ProtocolTests(unittest.TestCase): "a, (*b, c), d = (1, (2, 3, 4), 5) #@", Uninferable ) + def test_assigned_stmts_starred_inside_call(self) -> None: + """Regression test for https://github.com/PyCQA/pylint/issues/6372""" + code = "string_twos = ''.join(str(*y) for _, *y in [[1, 2], [1, 2]]) #@" + stmt = extract_node(code) + starred = next(stmt.nodes_of_class(nodes.Starred)) + starred_stmts = starred.assigned_stmts() + self.assertIs(next(starred_stmts), Uninferable) + # Generator exhausted after one call + with self.assertRaises(StopIteration): + next(starred_stmts) + def test_assign_stmts_starred_fails(self) -> None: # Too many starred self._helper_starred_inference_error("a, *b, *c = (1, 2, 3) #@") -- cgit v1.2.1