From e5731d3932f5f168cb6b224f705ac5695c03c467 Mon Sep 17 00:00:00 2001 From: Bryan Forbes Date: Mon, 15 Feb 2021 19:20:43 -0600 Subject: [PATCH] Only apply non-default params logic prior to the star (fixes #161) --- AUTHORS.txt | 1 + parso/python/errors.py | 23 ++++++++++++++----- test/normalizer_issue_files/allowed_syntax.py | 22 ++++++++++++++++++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/AUTHORS.txt b/AUTHORS.txt index 4ca3d0b..9737530 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -6,6 +6,7 @@ David Halter (@davidhalter) Code Contributors ================= Alisdair Robertson (@robodair) +Bryan Forbes (@bryanforbes) Code Contributors (to Jedi and therefore possibly to this library) diff --git a/parso/python/errors.py b/parso/python/errors.py index 364f72e..c67cc3f 100644 --- a/parso/python/errors.py +++ b/parso/python/errors.py @@ -160,7 +160,7 @@ def _skip_parens_bottom_up(node): def _iter_params(parent_node): - return (n for n in parent_node.children if n.type == 'param') + return (n for n in parent_node.children if n.type == 'param' or n.type == 'operator') def _is_future_import_first(import_from): @@ -942,17 +942,28 @@ class _ParameterRule(SyntaxRule): def is_issue(self, node): param_names = set() default_only = False + star_seen = False for p in _iter_params(node): + if p.type == 'operator': + if p.value == '*': + star_seen = True + default_only = False + continue + if p.name.value in param_names: message = "duplicate argument '%s' in function definition" self.add_issue(p.name, message=message % p.name.value) param_names.add(p.name.value) - if p.default is None and not p.star_count: - if default_only: - return True - else: - default_only = True + if not star_seen: + if p.default is None and not p.star_count: + if default_only: + return True + elif p.star_count: + star_seen = True + default_only = False + else: + default_only = True @ErrorFinder.register_rule(type='try_stmt') diff --git a/test/normalizer_issue_files/allowed_syntax.py b/test/normalizer_issue_files/allowed_syntax.py index 88df565..a83cede 100644 --- a/test/normalizer_issue_files/allowed_syntax.py +++ b/test/normalizer_issue_files/allowed_syntax.py @@ -46,6 +46,28 @@ def x(b=a): global a +def x(*args, c=2, d): + pass + + +def x(*, c=2, d): + pass + + +def x(a, b=1, *args, c=2, d): + pass + + +def x(a, b=1, *, c=2, d): + pass + + +lambda *args, c=2, d: (c, d) +lambda *, c=2, d: (c, d) +lambda a, b=1, *args, c=2, d: (c, d) +lambda a, b=1, *, c=2, d: (c, d) + + *foo, a = (1,) *foo[0], a = (1,) *[], a = (1,)