From 4a3f47ce8793cda198414b5aea7fae4c37cb43f9 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 24 Sep 2018 07:50:14 +0200 Subject: Handle recursively the count of Starred nodes in assignments --- pylint/checkers/base.py | 30 ++++++++++++---------- .../test/functional/too_many_star_expressions.py | 4 ++- .../test/functional/too_many_star_expressions.txt | 3 ++- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py index 4141f44cf..9f437e53b 100644 --- a/pylint/checkers/base.py +++ b/pylint/checkers/base.py @@ -533,23 +533,27 @@ class BasicErrorChecker(_BasicChecker): def visit_classdef(self, node): self._check_redefinition("class", node) + def _too_many_starred_for_tuple(self, tuple): + starred_count = 0 + for elem in tuple.itered(): + if isinstance(elem, astroid.Tuple): + return self._too_many_starred_for_tuple(elem) + elif isinstance(elem, astroid.Starred): + starred_count += 1 + return starred_count > 1 + @utils.check_messages("too-many-star-expressions", "invalid-star-assignment-target") def visit_assign(self, node): - target = node.targets[0] - - if isinstance(target, astroid.Tuple): - itered_elements = target.itered() - starred_count = 0 - for elem in itered_elements: - if isinstance(elem, astroid.Starred): - starred_count += 1 - elif sum(1 for _ in elem.nodes_of_class(astroid.Starred)) > 1: - self.add_message("too-many-star-expressions", node=node) - if starred_count > 1: - self.add_message("too-many-star-expressions", node=node) + # Check *a, *b = ... + assign_target = node.targets[0] + if not isinstance(assign_target, astroid.Tuple): + return + + if self._too_many_starred_for_tuple(assign_target): + self.add_message("too-many-star-expressions", node=node) # Check *a = b - if isinstance(target, astroid.Starred): + if isinstance(node.targets[0], astroid.Starred): self.add_message("invalid-star-assignment-target", node=node) @utils.check_messages("star-needs-assignment-target") diff --git a/pylint/test/functional/too_many_star_expressions.py b/pylint/test/functional/too_many_star_expressions.py index d107e276e..c1bbfb9d3 100644 --- a/pylint/test/functional/too_many_star_expressions.py +++ b/pylint/test/functional/too_many_star_expressions.py @@ -1,5 +1,7 @@ """Test for too-many-star-expressions.""" - +# pylint: disable=unbalanced-tuple-unpacking *FIRST, *SECOND = [1, 2, 3] # [too-many-star-expressions] (FIRST, *SECOND), *THIRD = ((1, 2, 3), 4, 5) *FIRST_1, SECOND = (1, 2, 3) +(*FIRST, *SECOND), THIRD = [...] # [too-many-star-expressions] +((*FIRST, SECOND), *THIRD), *FOURTH = [...] diff --git a/pylint/test/functional/too_many_star_expressions.txt b/pylint/test/functional/too_many_star_expressions.txt index c9b8b4f8d..97af35293 100644 --- a/pylint/test/functional/too_many_star_expressions.txt +++ b/pylint/test/functional/too_many_star_expressions.txt @@ -1 +1,2 @@ -too-many-star-expressions:3::More than one starred expression in assignment \ No newline at end of file +too-many-star-expressions:3::More than one starred expression in assignment +too-many-star-expressions:6::More than one starred expression in assignment -- cgit v1.2.1