From 2737dd2b3f6253c0921ee69391b9bf3e6c9590d0 Mon Sep 17 00:00:00 2001 From: openhands Date: Tue, 22 Apr 2025 14:18:30 +0000 Subject: [PATCH] Use empty string instead of 'No description available' for missing descriptions --- src/serena/mcp.py | 6 +++-- test/serena/test_mcp.py | 2 +- test_make_tool.py | 59 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 test_make_tool.py diff --git a/src/serena/mcp.py b/src/serena/mcp.py index 6bbfc5f0..f52e5292 100644 --- a/src/serena/mcp.py +++ b/src/serena/mcp.py @@ -64,9 +64,11 @@ def make_tool( if docstring.description: func_doc = f"{docstring.description.strip().strip('.')}." else: - func_doc = "No description available." + func_doc = "" if (docstring.returns) and (docstring_returns := docstring.returns.description): - func_doc = f"{func_doc} Returns {docstring_returns.strip().strip('.')}." + # Only add a space before "Returns" if func_doc is not empty + prefix = " " if func_doc else "" + func_doc = f"{func_doc}{prefix}Returns {docstring_returns.strip().strip('.')}." # Parse the parameter descriptions from the docstring and add pass its description # to the parameters schema. diff --git a/test/serena/test_mcp.py b/test/serena/test_mcp.py index 5998d23e..454b7b88 100644 --- a/test/serena/test_mcp.py +++ b/test/serena/test_mcp.py @@ -218,7 +218,7 @@ def test_make_tool_missing_description() -> None: mcp_tool = make_tool(tool) assert mcp_tool.name == "no_description" - assert mcp_tool.description == "No description available. Returns A result." + assert mcp_tool.description == "Returns A result." assert mcp_tool.parameters["properties"]["param"]["description"] == "The parameter." diff --git a/test_make_tool.py b/test_make_tool.py new file mode 100644 index 00000000..2933bd55 --- /dev/null +++ b/test_make_tool.py @@ -0,0 +1,59 @@ +"""Simple test for the make_tool function.""" + +import docstring_parser +from typing import Any, Callable + + +def make_tool_simplified(func_doc: str) -> str: + """Simplified version of make_tool function to test our fix.""" + docstring = docstring_parser.parse(func_doc) + + # Mount the tool description as a combination of the docstring description and + # the return value description, if it exists. + if docstring.description: + func_doc = f"{docstring.description.strip().strip('.')}." + else: + func_doc = "" + if (docstring.returns) and (docstring_returns := docstring.returns.description): + # Only add a space before "Returns" if func_doc is not empty + prefix = " " if func_doc else "" + func_doc = f"{func_doc}{prefix}Returns {docstring_returns.strip().strip('.')}." + + return func_doc + + +def test_with_description(): + """Test with a normal docstring that has a description.""" + doc = """This is a test function. + + :param name: The person's name + :param age: The person's age + :return: A greeting message + """ + result = make_tool_simplified(doc) + assert result == "This is a test function. Returns A greeting message." + + +def test_without_description(): + """Test with a docstring that has no description.""" + doc = """ + :param name: The person's name + :param age: The person's age + :return: A greeting message + """ + result = make_tool_simplified(doc) + assert result == "Returns A greeting message." + + +def test_empty_docstring(): + """Test with an empty docstring.""" + doc = "" + result = make_tool_simplified(doc) + assert result == "" + + +if __name__ == "__main__": + test_with_description() + test_without_description() + test_empty_docstring() + print("All tests passed!") \ No newline at end of file