Describe the bug
Walrus assignment can only assign to 1 assignment target, but the current implementation of extract function refactoring may potentially produce invalid code as the extracted code were attempting to assign the return value to multiple assignment target. There is a test case that was actually asserting for the invalid code, which will also need to be fixed:
|
@testutils.only_for_versions_higher("3.8") |
|
def test_extract_function_expression_with_inline_assignment_complex(self): |
|
code = dedent("""\ |
|
def foo(a): |
|
if i := a == (c := 5): |
|
i += 1 |
|
c += 1 |
|
print(i) |
|
""") |
|
extract_target = "i := a == (c := 5)" |
|
start, end = code.index(extract_target), code.index(extract_target) + len( |
|
extract_target |
|
) |
|
refactored = self.do_extract_method(code, start, end, "new_func") |
|
expected = dedent("""\ |
|
def foo(a): |
|
if i, c := new_func(a): |
|
i += 1 |
|
c += 1 |
|
print(i) |
|
|
|
def new_func(a): |
|
return (i := a == (c := 5)) |
|
""") |
|
self.assertEqual(expected, refactored) |
To Reproduce
Steps to reproduce the behavior:
- Code before refactoring:
def foo(a):
if i := a == (c := 5):
i += 1
c += 1
print(i)
-
Describe the refactoring you want to do: Extract "i := a == (c := 5)"
-
Expected code after refactoring, either produce something like:
def foo(a):
if (i := new_func(a, c := 5)):
i += 1
c += 1
print(i)
def new_func(a, c):
return (i := a == c)
or maybe should just refuse the extraction.
- Describe the error or unexpected result that you are getting:
def foo(a):
if i, c := new_func(a):
i += 1
c += 1
print(i)
def new_func(a):
return (i := a == (c := 5))
i, c := is not actually a valid assignment target for the walrus operator.
Editor information:
Describe the bug
Walrus assignment can only assign to 1 assignment target, but the current implementation of extract function refactoring may potentially produce invalid code as the extracted code were attempting to assign the return value to multiple assignment target. There is a test case that was actually asserting for the invalid code, which will also need to be fixed:
rope/ropetest/refactor/extracttest.py
Lines 2363 to 2387 in d2c5127
To Reproduce
Steps to reproduce the behavior:
Describe the refactoring you want to do: Extract "i := a == (c := 5)"
Expected code after refactoring, either produce something like:
or maybe should just refuse the extraction.
i, c :=is not actually a valid assignment target for the walrus operator.Editor information: