-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtest_docstring_parser.py
More file actions
197 lines (154 loc) · 5.56 KB
/
test_docstring_parser.py
File metadata and controls
197 lines (154 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""Tests for docstring parameter description parsing."""
from typing import Annotated
import pytest
from pydantic import Field
from mcp.server.mcpserver.utilities.docstring_parser import parse_docstring_params
from mcp.server.mcpserver.utilities.func_metadata import func_metadata
class TestGoogleStyle:
def test_basic(self):
doc = """Do something.
Args:
name: The name of the thing.
count: How many times.
"""
assert parse_docstring_params(doc) == {
"name": "The name of the thing.",
"count": "How many times.",
}
def test_with_type_annotations(self):
doc = """Do something.
Args:
name (str): The name of the thing.
count (int): How many times.
"""
assert parse_docstring_params(doc) == {
"name": "The name of the thing.",
"count": "How many times.",
}
def test_multiline_description(self):
doc = """Do something.
Args:
name: The name of the thing.
This is a longer description
that spans multiple lines.
count: How many times.
"""
result = parse_docstring_params(doc)
assert "longer description" in result["name"]
assert result["count"] == "How many times."
@pytest.mark.parametrize("keyword", ["Args", "Arguments", "Parameters"])
def test_section_keywords(self, keyword: str) -> None:
doc = f"""Do something.
{keyword}:
name: The name.
"""
assert parse_docstring_params(doc) == {"name": "The name."}
def test_stops_at_returns(self):
doc = """Do something.
Args:
name: The name.
Returns:
The result.
"""
assert parse_docstring_params(doc) == {"name": "The name."}
class TestNumpyStyle:
def test_basic(self):
doc = """Do something.
Parameters
----------
name : str
The name of the thing.
count : int
How many times.
"""
assert parse_docstring_params(doc) == {
"name": "The name of the thing.",
"count": "How many times.",
}
def test_multiline(self):
doc = """Do something.
Parameters
----------
name : str
The name of the thing.
More details here.
"""
assert "More details" in parse_docstring_params(doc)["name"]
class TestSphinxStyle:
def test_basic(self):
doc = """Do something.
:param name: The name of the thing.
:param count: How many times.
"""
assert parse_docstring_params(doc) == {
"name": "The name of the thing.",
"count": "How many times.",
}
def test_with_type(self):
doc = """Do something.
:param str name: The name of the thing.
"""
assert parse_docstring_params(doc) == {"name": "The name of the thing."}
class TestEdgeCases:
@pytest.mark.parametrize("doc", [None, "", "Just a description."])
def test_returns_empty(self, doc: str | None) -> None:
assert parse_docstring_params(doc) == {}
def test_google_section_with_no_valid_params(self):
doc = """Do something.
Args:
not a valid param line at all
another invalid line
"""
assert parse_docstring_params(doc) == {}
def test_numpy_section_with_no_valid_params(self):
doc = """Do something.
Parameters
----------
not a valid line
just some text
"""
assert parse_docstring_params(doc) == {}
class TestFuncMetadataIntegration:
def test_descriptions_appear_in_schema(self):
def my_tool(name: str, count: int = 5) -> str:
"""A tool.
Args:
name: The name to process.
count: Number of repetitions.
"""
return name * count # pragma: no cover
schema = func_metadata(my_tool).arg_model.model_json_schema()
assert schema["properties"]["name"]["description"] == "The name to process."
assert schema["properties"]["count"]["description"] == "Number of repetitions."
def test_explicit_field_takes_precedence(self):
def my_tool(
name: Annotated[str, Field(description="Explicit")],
count: int = 5,
) -> str:
"""A tool.
Args:
name: Should be ignored.
count: From docstring.
"""
return name * count # pragma: no cover
schema = func_metadata(my_tool).arg_model.model_json_schema()
assert schema["properties"]["name"]["description"] == "Explicit"
assert schema["properties"]["count"]["description"] == "From docstring."
def test_annotated_without_field_uses_docstring(self):
def my_tool(
name: Annotated[str, "just a string annotation"],
count: int = 5,
) -> str:
"""A tool.
Args:
name: From docstring.
count: Also docstring.
"""
return name * count # pragma: no cover
schema = func_metadata(my_tool).arg_model.model_json_schema()
assert schema["properties"]["name"]["description"] == "From docstring."
def test_no_docstring(self):
def my_tool(name: str) -> str:
return name # pragma: no cover
schema = func_metadata(my_tool).arg_model.model_json_schema()
assert "name" in schema["properties"]