From 45b965063ecbc6ce7fa86b6255eea3d28445bec4 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sun, 30 Jul 2017 18:42:36 +0200 Subject: [PATCH] Fix an issue with starred arguments and if they are defined or used. --- parso/python/normalizer.py | 18 +++++++++++++++--- test/normalizer_issue_files/allowed_syntax.py | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/parso/python/normalizer.py b/parso/python/normalizer.py index e067c0a..e3cbab8 100644 --- a/parso/python/normalizer.py +++ b/parso/python/normalizer.py @@ -286,8 +286,20 @@ class ErrorFinder(Normalizer): if node.parent.type == 'del_stmt': self._add_syntax_error("can't use starred expression here", node.parent) else: - is_definition = True - if is_definition: + def is_definition(node, ancestor): + if ancestor is None: + return False + + type_ = ancestor.type + if type_ == 'trailer': + return False + + if type_ == 'expr_stmt': + return node.start_pos < ancestor.children[-1].start_pos + + return is_definition(node, ancestor.parent) + + if is_definition(node, node.parent): args = [c for c in node.children if c != ','] starred = [c for c in args if c.type == 'star_expr'] if len(starred) > 1: @@ -304,7 +316,7 @@ class ErrorFinder(Normalizer): self._add_syntax_error(message, node) if node.parent.type == 'testlist_comp': # [*[] for a in [1]] - if node.parent.children[0] == node: + if node.parent.children[1].type == 'comp_for': message = "iterable unpacking cannot be used in comprehension" self._add_syntax_error(message, node) elif node.type == 'comp_for': diff --git a/test/normalizer_issue_files/allowed_syntax.py b/test/normalizer_issue_files/allowed_syntax.py index 4927e1b..4cae0d3 100644 --- a/test/normalizer_issue_files/allowed_syntax.py +++ b/test/normalizer_issue_files/allowed_syntax.py @@ -52,6 +52,8 @@ class X(): a = *args, *args +error[(*args, *args)] = 3 +*args, *args def glob():