Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Params should render themselves #16

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,8 @@ def expect_failure(self, block, err, *, filename=None, lineno=None):

def checkDocstring(self, fn, expected):
self.assertTrue(hasattr(fn, "docstring"))
self.assertEqual(fn.docstring.strip(),
dedent(expected).strip())
self.assertEqual(dedent(expected).strip(),
fn.docstring.strip())

def test_trivial(self):
parser = DSLParser(FakeClinic())
Expand Down Expand Up @@ -981,8 +981,12 @@ def test_function_docstring(self):

path: str
Path to be examined
Ensure that multiple lines are indented correctly.

Perform a stat system call on the given path.

Ensure that multiple lines are indented correctly.
Ensure that multiple lines are indented correctly.
""")
self.checkDocstring(function, """
stat($module, /, path)
Expand All @@ -992,6 +996,10 @@ def test_function_docstring(self):

path
Path to be examined
Ensure that multiple lines are indented correctly.

Ensure that multiple lines are indented correctly.
Ensure that multiple lines are indented correctly.
""")

def test_docstring_trailing_whitespace(self):
Expand Down
27 changes: 12 additions & 15 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2640,6 +2640,15 @@ def get_displayname(self, i: int) -> str:
else:
return f'"argument {i}"'

def render_docstring(self) -> str:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be a good case for -> str | None

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OTOH, I'm not sure it matters that much.

if not self.docstring:
return ""
add, out = text_accumulator()
add(f" {self.name}\n")
for line in self.docstring.split("\n"):
add(f" {line}\n")
return out().rstrip()


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

Expand Down Expand Up @@ -5483,21 +5492,9 @@ def add_parameter(text: str) -> None:
def format_docstring_parameters(params: list[Parameter]) -> str:
"""Create substitution text for {parameters}"""
text, add, output = _text_accumulator()
spacer_line = False
for param in params:
docstring = param.docstring.strip()
if not docstring:
continue
if spacer_line:
add('\n')
else:
spacer_line = True
add(" ")
add(param.name)
add('\n')
stripped = rstrip_lines(docstring.rstrip())
add(textwrap.indent(stripped, " "))
if text:
docstrings = [p.render_docstring() for p in params if p.docstring]
for docstring in docstrings:
add(docstring)
add('\n')
return output()

Expand Down
Loading