Fix extract_function when the extracted code defines no variable

If the extracted lines define no name, the new function ended with a
bare "return " and the call site became " = func(v1)". Both are
invalid Python.

Only add the final return line and the assignment when there is at
least one variable to return. Otherwise call the function as a
statement.
This commit is contained in:
spicysharkco committed 2026-10-18 02:07:55 +00:00
1 parent 2af1a0f394
commit 897cbff748
3 files changed
+22 -2

No files matched your search

+5 -2
View File
@@ -247,7 +247,8 @@ def extract_function(inference_state, path, module_context, name, pos, until_pos
code_block = dedent(code_block)
if not has_ending_return_stmt:
output_var_str = ', '.join(return_variables)
code_block += 'return ' + output_var_str + '\n'
if return_variables:
code_block += 'return ' + output_var_str + '\n'
# Check if we have to raise RefactoringError
_check_for_non_extractables(nodes[:-1] if has_ending_return_stmt else nodes)
@@ -282,8 +283,10 @@ def extract_function(inference_state, path, module_context, name, pos, until_pos
else:
if has_ending_return_stmt:
replacement = 'return ' + function_call + '\n'
else:
elif return_variables:
replacement = output_var_str + ' = ' + function_call + '\n'
else:
replacement = function_call + '\n'
replacement_dct = _replace(nodes, replacement, function_code, pos,
insert_before_leaf, remaining_prefix)