Skip to content

Commit 7ff262e

Browse files
Test: refactor format_docstring()
1 parent 3d9d6c6 commit 7ff262e

File tree

1 file changed

+36
-43
lines changed

1 file changed

+36
-43
lines changed

Tools/clinic/clinic.py

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ class _TextAccumulator(NamedTuple):
111111

112112
def _text_accumulator() -> _TextAccumulator:
113113
text: list[str] = []
114+
def peak() -> str:
115+
return ''.join(text)
114116
def output() -> str:
115117
s = ''.join(text)
116118
text.clear()
@@ -5318,23 +5320,13 @@ def state_function_docstring(self, line: str) -> None:
53185320

53195321
self.docstring_append(self.function, line)
53205322

5321-
def format_docstring(self) -> str:
5322-
f = self.function
5323-
assert f is not None
5324-
5325-
new_or_init = f.kind.new_or_init
5326-
if new_or_init and not f.docstring:
5327-
# don't render a docstring at all, no signature, nothing.
5328-
return f.docstring
5329-
5323+
def format_docstring_signature(
5324+
self,
5325+
f: Function,
5326+
parameters: list[Parameter]
5327+
) -> str:
53305328
text, add, output = _text_accumulator()
5331-
parameters = f.render_parameters
5332-
5333-
##
5334-
## docstring first line
5335-
##
5336-
5337-
if new_or_init:
5329+
if f.kind.new_or_init:
53385330
# classes get *just* the name of the class
53395331
# not __new__, not __init__, and not module.classname
53405332
assert f.cls
@@ -5494,35 +5486,38 @@ def add_parameter(text: str) -> None:
54945486
if not f.docstring_only:
54955487
add("\n" + sig_end_marker + "\n")
54965488

5497-
docstring_first_line = output()
5489+
signature_line = output()
54985490

54995491
# now fix up the places where the brackets look wrong
5500-
docstring_first_line = docstring_first_line.replace(', ]', ',] ')
5492+
return signature_line.replace(', ]', ',] ')
55015493

5502-
# okay. now we're officially building the "parameters" section.
5503-
# create substitution text for {parameters}
5494+
def format_docstring_parameters(self, params: list[Parameter]) -> str:
5495+
"""Create substitution text for {parameters}"""
5496+
text, add, output = _text_accumulator()
55045497
spacer_line = False
5505-
for p in parameters:
5506-
if not p.docstring.strip():
5498+
for param in params:
5499+
docstring = param.docstring.strip()
5500+
if not docstring:
55075501
continue
55085502
if spacer_line:
55095503
add('\n')
55105504
else:
55115505
spacer_line = True
55125506
add(" ")
5513-
add(p.name)
5507+
add(param.name)
5508+
add('\n')
5509+
add(textwrap.indent(rstrip_lines(docstring), " "))
5510+
if text:
55145511
add('\n')
5515-
add(textwrap.indent(rstrip_lines(p.docstring.rstrip()), " "))
5516-
parameters_output = output()
5517-
if parameters_output:
5518-
parameters_output += '\n'
5512+
return output()
55195513

5520-
##
5521-
## docstring body
5522-
##
5514+
def format_docstring(self) -> str:
5515+
f = self.function
5516+
assert f is not None
55235517

5524-
docstring = f.docstring.rstrip()
5525-
lines = [line.rstrip() for line in docstring.split('\n')]
5518+
if f.kind.new_or_init and not f.docstring:
5519+
# don't render a docstring at all, no signature, nothing.
5520+
return f.docstring
55265521

55275522
# Enforce the summary line!
55285523
# The first line of a docstring should be a summary of the function.
@@ -5536,6 +5531,7 @@ def add_parameter(text: str) -> None:
55365531
# Guido said Clinic should enforce this:
55375532
# http://mail.python.org/pipermail/python-dev/2013-June/127110.html
55385533

5534+
lines = [line for line in f.docstring.split('\n')]
55395535
if len(lines) >= 2:
55405536
if lines[1]:
55415537
fail("Docstring for " + f.full_name + " does not have a summary line!\n" +
@@ -5547,26 +5543,23 @@ def add_parameter(text: str) -> None:
55475543
# between it and the {parameters} we're about to add.
55485544
lines.append('')
55495545

5550-
parameters_marker_count = len(docstring.split('{parameters}')) - 1
5546+
parameters_marker_count = len(f.docstring.split('{parameters}')) - 1
55515547
if parameters_marker_count > 1:
55525548
fail('You may not specify {parameters} more than once in a docstring!')
55535549

55545550
if not parameters_marker_count:
55555551
# insert after summary line
55565552
lines.insert(2, '{parameters}')
55575553

5558-
# insert at front of docstring
5559-
lines.insert(0, docstring_first_line)
5554+
# insert signature at front of docstring
5555+
params = f.render_parameters
5556+
signature = self.format_docstring_signature(f, params)
5557+
lines.insert(0, signature)
55605558

5559+
# add parameters section and finalize docstring
5560+
parameters = self.format_docstring_parameters(params)
55615561
docstring = "\n".join(lines)
5562-
5563-
add(docstring)
5564-
docstring = output()
5565-
5566-
docstring = linear_format(docstring, parameters=parameters_output)
5567-
docstring = docstring.rstrip()
5568-
5569-
return docstring
5562+
return linear_format(docstring, parameters=parameters).rstrip()
55705563

55715564
def do_post_block_processing_cleanup(self) -> None:
55725565
"""

0 commit comments

Comments
 (0)