Skip to content

Commit

Permalink
Only apply non-default params logic prior to the star (fixes #161)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanforbes committed Feb 16, 2021
1 parent 93206f6 commit e5731d3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ David Halter (@davidhalter) <davidhalter88@gmail.com>
Code Contributors
=================
Alisdair Robertson (@robodair)
Bryan Forbes (@bryanforbes) <bryan@reigndropsfall.net>


Code Contributors (to Jedi and therefore possibly to this library)
Expand Down
23 changes: 17 additions & 6 deletions parso/python/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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')
Expand Down
22 changes: 22 additions & 0 deletions test/normalizer_issue_files/allowed_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,)
Expand Down

0 comments on commit e5731d3

Please sign in to comment.