Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with parsing function call args list #507

Merged
merged 2 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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