Skip to content

Commit 1b3f5f2

Browse files
gh-104683: Argument Clinic: Params now render their own docstrings (#107790)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 65ce365 commit 1b3f5f2

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

Lib/test/test_clinic.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -833,8 +833,8 @@ def expect_failure(self, block, err, *, filename=None, lineno=None):
833833

834834
def checkDocstring(self, fn, expected):
835835
self.assertTrue(hasattr(fn, "docstring"))
836-
self.assertEqual(fn.docstring.strip(),
837-
dedent(expected).strip())
836+
self.assertEqual(dedent(expected).strip(),
837+
fn.docstring.strip())
838838

839839
def test_trivial(self):
840840
parser = DSLParser(_make_clinic())
@@ -997,8 +997,12 @@ def test_function_docstring(self):
997997
998998
path: str
999999
Path to be examined
1000+
Ensure that multiple lines are indented correctly.
10001001
10011002
Perform a stat system call on the given path.
1003+
1004+
Ensure that multiple lines are indented correctly.
1005+
Ensure that multiple lines are indented correctly.
10021006
""")
10031007
self.checkDocstring(function, """
10041008
stat($module, /, path)
@@ -1008,6 +1012,10 @@ def test_function_docstring(self):
10081012
10091013
path
10101014
Path to be examined
1015+
Ensure that multiple lines are indented correctly.
1016+
1017+
Ensure that multiple lines are indented correctly.
1018+
Ensure that multiple lines are indented correctly.
10111019
""")
10121020

10131021
def test_docstring_trailing_whitespace(self):

Tools/clinic/clinic.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -2775,6 +2775,13 @@ def get_displayname(self, i: int) -> str:
27752775
else:
27762776
return f'"argument {i}"'
27772777

2778+
def render_docstring(self) -> str:
2779+
add, out = text_accumulator()
2780+
add(f" {self.name}\n")
2781+
for line in self.docstring.split("\n"):
2782+
add(f" {line}\n")
2783+
return out().rstrip()
2784+
27782785

27792786
CConverterClassT = TypeVar("CConverterClassT", bound=type["CConverter"])
27802787

@@ -5686,23 +5693,11 @@ def add_parameter(text: str) -> None:
56865693
@staticmethod
56875694
def format_docstring_parameters(params: list[Parameter]) -> str:
56885695
"""Create substitution text for {parameters}"""
5689-
text, add, output = _text_accumulator()
5690-
spacer_line = False
5691-
for param in params:
5692-
docstring = param.docstring.strip()
5693-
if not docstring:
5694-
continue
5695-
if spacer_line:
5696+
add, output = text_accumulator()
5697+
for p in params:
5698+
if p.docstring:
5699+
add(p.render_docstring())
56965700
add('\n')
5697-
else:
5698-
spacer_line = True
5699-
add(" ")
5700-
add(param.name)
5701-
add('\n')
5702-
stripped = rstrip_lines(docstring)
5703-
add(textwrap.indent(stripped, " "))
5704-
if text:
5705-
add('\n')
57065701
return output()
57075702

57085703
def format_docstring(self) -> str:

0 commit comments

Comments
 (0)