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 posonlyargs and kwonlyargs when extracting method #597

Merged
merged 7 commits into from Dec 14, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@
- #559 Improve handling of whitespace in import and from-import statements
- #581 Remove functions in rope.base.ast that has functionally identical implementation in stdlib's ast
- #589 Fix issue with `sample_project()` creating directories where it shouldn't when running tests
- #566, #567 Fix variables in kwonlyargs and posonlyargs not being correctly passed to extracted methods

# Release 1.5.1

Expand Down
5 changes: 4 additions & 1 deletion rope/refactor/extract.py
Expand Up @@ -947,11 +947,14 @@ def _handle_loop_context(self, node):


def _get_argnames(arguments):
result = [node.arg for node in arguments.args if isinstance(node, ast.arg)]
result = []
result.extend(node.arg for node in getattr(arguments, "posonlyargs", []))
result.extend(node.arg for node in arguments.args)
if arguments.vararg:
result.append(arguments.vararg.arg)
if arguments.kwarg:
result.append(arguments.kwarg.arg)
result.extend(node.arg for node in arguments.kwonlyargs)
return result


Expand Down
37 changes: 37 additions & 0 deletions ropetest/refactor/extracttest.py
Expand Up @@ -204,6 +204,43 @@ def new_func():
""")
self.assertEqual(expected, refactored)

def test_extract_function_with_kwonlyargs(self):
code = dedent("""\
def a_func(b, *, a_var):
another_var = 20
third_var = a_var + another_var
""")
start, end = self._convert_line_range_to_offset(code, 3, 3)
refactored = self.do_extract_method(code, start, end, "new_func")
expected = dedent("""\
def a_func(b, *, a_var):
another_var = 20
new_func(a_var, another_var)

def new_func(a_var, another_var):
third_var = a_var + another_var
""")
self.assertEqual(expected, refactored)

@testutils.only_for_versions_higher("3.8")
def test_extract_function_with_posonlyargs(self):
code = dedent("""\
def a_func(a_var, /, b):
another_var = 20
third_var = a_var + another_var
""")
start, end = self._convert_line_range_to_offset(code, 3, 3)
refactored = self.do_extract_method(code, start, end, "new_func")
expected = dedent("""\
def a_func(a_var, /, b):
another_var = 20
new_func(a_var, another_var)

def new_func(a_var, another_var):
third_var = a_var + another_var
""")
self.assertEqual(expected, refactored)

def test_extract_function_with_multiple_return_values(self):
code = dedent("""\
def a_func():
Expand Down