@@ -111,6 +111,8 @@ class _TextAccumulator(NamedTuple):
111
111
112
112
def _text_accumulator () -> _TextAccumulator :
113
113
text : list [str ] = []
114
+ def peak () -> str :
115
+ return '' .join (text )
114
116
def output () -> str :
115
117
s = '' .join (text )
116
118
text .clear ()
@@ -5318,23 +5320,13 @@ def state_function_docstring(self, line: str) -> None:
5318
5320
5319
5321
self .docstring_append (self .function , line )
5320
5322
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 :
5330
5328
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 :
5338
5330
# classes get *just* the name of the class
5339
5331
# not __new__, not __init__, and not module.classname
5340
5332
assert f .cls
@@ -5494,35 +5486,38 @@ def add_parameter(text: str) -> None:
5494
5486
if not f .docstring_only :
5495
5487
add ("\n " + sig_end_marker + "\n " )
5496
5488
5497
- docstring_first_line = output ()
5489
+ signature_line = output ()
5498
5490
5499
5491
# now fix up the places where the brackets look wrong
5500
- docstring_first_line = docstring_first_line .replace (', ]' , ',] ' )
5492
+ return signature_line .replace (', ]' , ',] ' )
5501
5493
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 ()
5504
5497
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 :
5507
5501
continue
5508
5502
if spacer_line :
5509
5503
add ('\n ' )
5510
5504
else :
5511
5505
spacer_line = True
5512
5506
add (" " )
5513
- add (p .name )
5507
+ add (param .name )
5508
+ add ('\n ' )
5509
+ add (textwrap .indent (rstrip_lines (docstring ), " " ))
5510
+ if text :
5514
5511
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 ()
5519
5513
5520
- ##
5521
- ## docstring body
5522
- ##
5514
+ def format_docstring ( self ) -> str :
5515
+ f = self . function
5516
+ assert f is not None
5523
5517
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
5526
5521
5527
5522
# Enforce the summary line!
5528
5523
# The first line of a docstring should be a summary of the function.
@@ -5536,6 +5531,7 @@ def add_parameter(text: str) -> None:
5536
5531
# Guido said Clinic should enforce this:
5537
5532
# http://mail.python.org/pipermail/python-dev/2013-June/127110.html
5538
5533
5534
+ lines = [line for line in f .docstring .split ('\n ' )]
5539
5535
if len (lines ) >= 2 :
5540
5536
if lines [1 ]:
5541
5537
fail ("Docstring for " + f .full_name + " does not have a summary line!\n " +
@@ -5547,26 +5543,23 @@ def add_parameter(text: str) -> None:
5547
5543
# between it and the {parameters} we're about to add.
5548
5544
lines .append ('' )
5549
5545
5550
- parameters_marker_count = len (docstring .split ('{parameters}' )) - 1
5546
+ parameters_marker_count = len (f . docstring .split ('{parameters}' )) - 1
5551
5547
if parameters_marker_count > 1 :
5552
5548
fail ('You may not specify {parameters} more than once in a docstring!' )
5553
5549
5554
5550
if not parameters_marker_count :
5555
5551
# insert after summary line
5556
5552
lines .insert (2 , '{parameters}' )
5557
5553
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 )
5560
5558
5559
+ # add parameters section and finalize docstring
5560
+ parameters = self .format_docstring_parameters (params )
5561
5561
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 ()
5570
5563
5571
5564
def do_post_block_processing_cleanup (self ) -> None :
5572
5565
"""
0 commit comments