From 1912551296d23cee6cf1dcec469c31f116df3497 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sat, 19 Aug 2017 12:59:49 +0200 Subject: [PATCH] Move some star_expr checking around. --- parso/normalizer.py | 20 +++++++------- parso/python/errors.py | 60 ++++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/parso/normalizer.py b/parso/normalizer.py index d200c86..0a7cbbd 100644 --- a/parso/normalizer.py +++ b/parso/normalizer.py @@ -87,20 +87,20 @@ class Normalizer(use_metaclass(_NormalizerMeta)): @classmethod def _register_rule(cls, value=None, values=(), type=None, types=()): - if value is None and type is None: + values = list(values) + types = list(types) + if value is not None: + values.append(value) + if type is not None: + types.append(type) + + if not values and not types: raise ValueError("You must register at least something.") def decorator(rule_cls): - copied_values = list(values) - copied_types = list(types) - if value is not None: - copied_values.append(value) - if type is not None: - copied_types.append(type) - - for v in copied_values: + for v in values: cls.rule_value_classes.setdefault(v, []).append(rule_cls) - for t in copied_types: + for t in types: cls.rule_type_classes.setdefault(t, []).append(rule_cls) return rule_cls diff --git a/parso/python/errors.py b/parso/python/errors.py index c12c88e..c76425e 100644 --- a/parso/python/errors.py +++ b/parso/python/errors.py @@ -289,34 +289,6 @@ class ErrorFinder(Normalizer): self._add_syntax_error(node, "too many statically nested blocks") yield return - elif node.type in _STAR_EXPR_PARENTS: - if node.parent.type == 'del_stmt': - self._add_syntax_error(node.parent, "can't use starred expression here") - else: - 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: - message = "two starred expressions in assignment" - self._add_syntax_error(starred[1], message) - elif starred: - count = args.index(starred[0]) - if count >= 256: - message = "too many expressions in star-unpacking assignment" - self._add_syntax_error(starred[0], message) elif node.type == 'suite': self._indentation_count += 1 if self._indentation_count == _MAX_INDENT_COUNT: @@ -678,6 +650,38 @@ class _StarExprRule(SyntaxRule): self.add_issue(node, message=self.message_assignment) +@ErrorFinder.register_rule(types=_STAR_EXPR_PARENTS) +class _StarExprParentRule(SyntaxRule): + def is_issue(self, node): + if node.parent.type == 'del_stmt': + self.add_issue(node.parent, message="can't use starred expression here") + else: + 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: + message = "two starred expressions in assignment" + self.add_issue(starred[1], message=message) + elif starred: + count = args.index(starred[0]) + if count >= 256: + message = "too many expressions in star-unpacking assignment" + self.add_issue(starred[0], message=message) + + @ErrorFinder.register_rule(type='annassign') class _AnnotatorRule(SyntaxRule): # True: int