Skip to content

Commit

Permalink
Merge pull request #507 from python-rope/lieryan-506-parsing-starred-…
Browse files Browse the repository at this point in the history
…args

Fix issue with parsing function call args list
  • Loading branch information
lieryan committed Oct 5, 2022
2 parents 2733daa + d21d98c commit 2fa7726
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 30 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Bug fixes

- #411 Fix extracting generator without parens
- #506,#507 Fix extracting generator without parens
- #411,#505 Fix extracting generator without parens

# Release 1.3.0

Expand Down
30 changes: 2 additions & 28 deletions rope/refactor/patchedast.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,34 +352,8 @@ def _arg_sort_key(node):
return (node.lineno, node.col_offset)

children = [node.func, "("]
unstarred_args = []
starred_and_keywords = list(node.keywords)
for i, arg in enumerate(node.args):
if hasattr(ast, "Starred") and isinstance(arg, ast.Starred):
starred_and_keywords.append(arg)
else:
unstarred_args.append(arg)
if getattr(node, "starargs", None):
starred_and_keywords.append(node.starargs)
starred_and_keywords.sort(key=_arg_sort_key)
children.extend(self._child_nodes(unstarred_args, ","))

# positional args come before keywords, *args comes after all
# positional args, and **kwargs comes last
if starred_and_keywords:
if len(children) > 2:
children.append(",")
for i, arg in enumerate(starred_and_keywords):
if arg == getattr(node, "starargs", None):
children.append("*")
children.append(arg)
if i + 1 < len(starred_and_keywords):
children.append(",")

if getattr(node, "kwargs", None):
if len(children) > 2:
children.append(",")
children.extend(["**", node.kwargs])
args = sorted([*node.args, *node.keywords], key=_arg_sort_key)
children.extend(self._child_nodes(args, ","))
children.append(")")
self._handle(node, children)

Expand Down
12 changes: 11 additions & 1 deletion ropetest/refactor/patchedasttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ def test_try_except_node__with_as_syntax(self):
node_to_test,
["try", "", ":", "\n ", "Pass", "\n", ("excepthandler", "ExceptHandler")],
)
expected_child = "e"
expected_child = "e"
checker.check_children(
("excepthandler", "ExceptHandler"),
["except", " ", "Name", " ", "as", " ", expected_child, "", ":", "\n ", "Pass"],
Expand Down Expand Up @@ -1341,6 +1341,16 @@ def test_starargs_in_keywords(self):
["Name", "", "(", "", "keyword", "", ",", " *", "Starred", "", ",", " ", "keyword", "", ")"],
)

@testutils.only_for("3.5")
def test_starargs_in_positional(self):
source = "foo(a, *b, c)\n"
ast_frag = patchedast.get_patched_ast(source, True)
checker = _ResultChecker(self, ast_frag)
checker.check_children(
"Call",
["Name", "", "(", "", "Name", "", ",", " *", "Starred", "", ",", " ", "Name", "", ")"],
)

@testutils.only_for("3.5")
def test_starargs_after_keywords(self):
source = "foo(a=1, *args)\n"
Expand Down

0 comments on commit 2fa7726

Please sign in to comment.