Skip to content

Commit

Permalink
Naive fix for return statements
Browse files Browse the repository at this point in the history
Signed-off-by: Ahdra Merali <ahdra.merali@quantumblack.com>
  • Loading branch information
Ahdra Merali committed Feb 1, 2024
1 parent 79f7e4f commit 04c022e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
25 changes: 22 additions & 3 deletions kedro/ipython/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,31 @@ def _prepare_function_body(func: Callable) -> str:
# https://stackoverflow.com/questions/38050649/getting-a-python-functions-source-code-without-the-definition-lines
all_source_lines = inspect.getsourcelines(func)[0]
# Remove any decorators
func_lines = dropwhile(lambda x: x.startswith("@"), all_source_lines)
line: str = next(func_lines).strip()
raw_func_lines = dropwhile(lambda x: x.startswith("@"), all_source_lines)
line: str = next(raw_func_lines).strip()
if not line.startswith("def "):
return line.rsplit(",")[0].strip()
# Handle functions that are not one-liners
first_line = next(func_lines)
first_line = next(raw_func_lines)

# Convert return statements into print statements
func_lines = []
for line in raw_func_lines:
if line.lstrip().startswith("return"):
return_statement = line.lstrip()
line_indentation = len(line) - len(return_statement)
commented_return = line[:line_indentation] + "# " + return_statement
printed_return = (
line[:line_indentation]
+ "print("
+ return_statement[len("return") :].strip()
+ ")"
)
func_lines.append(commented_return)
func_lines.append(printed_return)
else:
func_lines.append(line)

# Find the indentation of the first line
indentation = len(first_line) - len(first_line.lstrip())
body = "".join(
Expand Down
9 changes: 6 additions & 3 deletions tests/ipython/test_ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ def test_load_node(self, mocker, dummy_function_file_lines, dummy_pipelines):
# this is an in-line comment in the body of the function
random_assignment = "Added for a longer function"
random_assignment += "make sure to modify variable"
return not dummy_input"""
# return not dummy_input
print(not dummy_input)"""

expected_cells = [
node_inputs,
Expand Down Expand Up @@ -492,7 +493,8 @@ def test_prepare_function_body(self):
# this is an in-line comment in the body of the function
random_assignment = "Added for a longer function"
random_assignment += "make sure to modify variable"
return not dummy_input"""
# return not dummy_input
print(not dummy_input)"""

result = _prepare_function_body(dummy_function)
assert result == func_strings
Expand All @@ -515,7 +517,8 @@ def test_get_nested_function_body(self):
def test_get_function_with_loop_body(self):
func_strings = """for x in dummy_list:
continue
return len(dummy_list)"""
# return len(dummy_list)
print(len(dummy_list))"""

result = _prepare_function_body(dummy_function_with_loop)
assert result == func_strings

0 comments on commit 04c022e

Please sign in to comment.