Merge pull request #2122 from spicysharkco/fix/extract-function-no-output

Fix extract_function when the extracted code defines no variable
This commit is contained in:
Dave Halter authored and GitHub committed 2026-10-18 07:40:13 +00:00
commit 65112c72c7
3 files changed
+22 -2

No files matched your search

+1
View File
@@ -71,6 +71,7 @@ Code Contributors
- Dhanraj E (@Dhanu-dynamic) <dhanraj@labs.digiscrypt.com>
- Charan Rathore (@charan-rathore)
- Abdullah Ahmed (@abdull-ah-med)
- spicysharkco (@spicysharkco)
And a few more "anonymous" contributors.
+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)
+16
View File
@@ -314,6 +314,22 @@ def x(v1):
#bar
return x
x
# -------------------------------------------------- with-range-func-no-output
def x(v1):
#? 2 text {'new_name': 'func', 'until_line': 4}
print(v1)
print(v1 + 1)
return v1
# ++++++++++++++++++++++++++++++++++++++++++++++++++
def func(v1):
print(v1)
print(v1 + 1)
def x(v1):
#? 2 text {'new_name': 'func', 'until_line': 4}
func(v1)
return v1
# -------------------------------------------------- in-class-range-1
class X1:
#? 9 text {'new_name': 'f', 'until_line': 4}