Skip to content

Commit

Permalink
Strip function body of CodeInput (#92)
Browse files Browse the repository at this point in the history
This removes the linebreak at the end which results in undesired
formatting in the `CodeInput`.
  • Loading branch information
agoscinski authored Dec 17, 2024
1 parent a8a21d0 commit 99a24f6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/scwidgets/code/_widget_code_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def get_function_body(function: types.FunctionType) -> str:
line[leading_indent:] if line.strip() else "" for line in lines
)

return source
return source.strip()

@property
def function(self) -> types.FunctionType:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ def test_get_docstring(self):
def test_get_function_body(self):
assert (
CodeInput.get_function_body(self.mock_function_1)
== "if x > 0:\n return x + y\nelse:\n return y + z()\n"
== "if x > 0:\n return x + y\nelse:\n return y + z()"
)
assert CodeInput.get_function_body(self.mock_function_2) == "return x\n"
assert CodeInput.get_function_body(self.mock_function_3) == "return x\n"
assert CodeInput.get_function_body(self.mock_function_2) == "return x"
assert CodeInput.get_function_body(self.mock_function_3) == "return x"
assert (
CodeInput.get_function_body(self.mock_function_4)
== "return x # noqa: E702\n"
== "return x # noqa: E702"
)
assert (
CodeInput.get_function_body(self.mock_function_5)
== "def x():\n return 5\nreturn x()\n"
== "def x():\n return 5\nreturn x()"
)
assert CodeInput.get_function_body(self.mock_function_6) == "return x\n"
assert CodeInput.get_function_body(self.mock_function_6) == "return x"
with pytest.raises(
ValueError,
match=r"Did not find any def definition. .*",
Expand Down

0 comments on commit 99a24f6

Please sign in to comment.