-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathgoogle.py
905 lines (751 loc) · 31.7 KB
/
google.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
# This module defines functions to parse Google-style docstrings into structured data.
from __future__ import annotations
import re
from contextlib import suppress
from typing import TYPE_CHECKING
from _griffe.docstrings.models import (
DocstringAttribute,
DocstringClass,
DocstringFunction,
DocstringModule,
DocstringParameter,
DocstringRaise,
DocstringReceive,
DocstringReturn,
DocstringSection,
DocstringSectionAdmonition,
DocstringSectionAttributes,
DocstringSectionClasses,
DocstringSectionDeprecated,
DocstringSectionExamples,
DocstringSectionFunctions,
DocstringSectionModules,
DocstringSectionOtherParameters,
DocstringSectionParameters,
DocstringSectionRaises,
DocstringSectionReceives,
DocstringSectionReturns,
DocstringSectionText,
DocstringSectionWarns,
DocstringSectionYields,
DocstringWarn,
DocstringYield,
)
from _griffe.docstrings.utils import docstring_warning, parse_docstring_annotation
from _griffe.enumerations import DocstringSectionKind, LogLevel
if TYPE_CHECKING:
from re import Pattern
from typing import Any, Literal
from _griffe.expressions import Expr
from _griffe.models import Docstring
_section_kind = {
"args": DocstringSectionKind.parameters,
"arguments": DocstringSectionKind.parameters,
"params": DocstringSectionKind.parameters,
"parameters": DocstringSectionKind.parameters,
"keyword args": DocstringSectionKind.other_parameters,
"keyword arguments": DocstringSectionKind.other_parameters,
"other args": DocstringSectionKind.other_parameters,
"other arguments": DocstringSectionKind.other_parameters,
"other params": DocstringSectionKind.other_parameters,
"other parameters": DocstringSectionKind.other_parameters,
"raises": DocstringSectionKind.raises,
"exceptions": DocstringSectionKind.raises,
"returns": DocstringSectionKind.returns,
"yields": DocstringSectionKind.yields,
"receives": DocstringSectionKind.receives,
"examples": DocstringSectionKind.examples,
"attributes": DocstringSectionKind.attributes,
"functions": DocstringSectionKind.functions,
"methods": DocstringSectionKind.functions,
"classes": DocstringSectionKind.classes,
"modules": DocstringSectionKind.modules,
"warns": DocstringSectionKind.warns,
"warnings": DocstringSectionKind.warns,
}
_BlockItem = tuple[int, list[str]]
_BlockItems = list[_BlockItem]
_ItemsBlock = tuple[_BlockItems, int]
_RE_ADMONITION: Pattern = re.compile(r"^(?P<type>[\w][\s\w-]*):(\s+(?P<title>[^\s].*))?\s*$", re.IGNORECASE)
_RE_NAME_ANNOTATION_DESCRIPTION: Pattern = re.compile(r"^(?:(?P<name>\w+)?\s*(?:\((?P<type>.+)\))?:\s*)?(?P<desc>.*)$")
_RE_DOCTEST_BLANKLINE: Pattern = re.compile(r"^\s*<BLANKLINE>\s*$")
_RE_DOCTEST_FLAGS: Pattern = re.compile(r"(\s*#\s*doctest:.+)$")
def _read_block_items(docstring: Docstring, *, offset: int, **options: Any) -> _ItemsBlock: # noqa: ARG001
lines = docstring.lines
if offset >= len(lines):
return [], offset
new_offset = offset
items: _BlockItems = []
# skip first empty lines
while _is_empty_line(lines[new_offset]):
new_offset += 1
# get initial indent
indent = len(lines[new_offset]) - len(lines[new_offset].lstrip())
if indent == 0:
# first non-empty line was not indented, abort
return [], new_offset - 1
# start processing first item
current_item = (new_offset, [lines[new_offset][indent:]])
new_offset += 1
# loop on next lines
while new_offset < len(lines):
line = lines[new_offset]
if _is_empty_line(line):
# empty line: preserve it in the current item
current_item[1].append("")
elif line.startswith(indent * 2 * " "):
# continuation line
current_item[1].append(line[indent * 2 :])
elif line.startswith((indent + 1) * " "):
# indent between initial and continuation: append but warn
cont_indent = len(line) - len(line.lstrip())
current_item[1].append(line[cont_indent:])
docstring_warning(
docstring,
new_offset,
f"Confusing indentation for continuation line {new_offset+1} in docstring, "
f"should be {indent} * 2 = {indent*2} spaces, not {cont_indent}",
)
elif line.startswith(indent * " "):
# indent equal to initial one: new item
items.append(current_item)
current_item = (new_offset, [line[indent:]])
else:
# indent lower than initial one: end of section
break
new_offset += 1
if current_item:
items.append(current_item)
return items, new_offset - 1
def _read_block(docstring: Docstring, *, offset: int, **options: Any) -> tuple[str, int]: # noqa: ARG001
lines = docstring.lines
if offset >= len(lines):
return "", offset - 1
new_offset = offset
block: list[str] = []
# skip first empty lines
while _is_empty_line(lines[new_offset]):
new_offset += 1
# get initial indent
indent = len(lines[new_offset]) - len(lines[new_offset].lstrip())
if indent == 0:
# first non-empty line was not indented, abort
return "", offset - 1
# start processing first item
block.append(lines[new_offset].lstrip())
new_offset += 1
# loop on next lines
while new_offset < len(lines) and (lines[new_offset].startswith(indent * " ") or _is_empty_line(lines[new_offset])):
block.append(lines[new_offset][indent:])
new_offset += 1
return "\n".join(block).rstrip("\n"), new_offset - 1
def _read_parameters(
docstring: Docstring,
*,
offset: int,
warn_unknown_params: bool = True,
**options: Any,
) -> tuple[list[DocstringParameter], int]:
parameters = []
annotation: str | Expr | None
block, new_offset = _read_block_items(docstring, offset=offset, **options)
for line_number, param_lines in block:
# check the presence of a name and description, separated by a colon
try:
name_with_type, description = param_lines[0].split(":", 1)
except ValueError:
docstring_warning(docstring, line_number, f"Failed to get 'name: description' pair from '{param_lines[0]}'")
continue
description = "\n".join([description.lstrip(), *param_lines[1:]]).rstrip("\n")
# use the type given after the parameter name, if any
if " " in name_with_type:
name, annotation = name_with_type.split(" ", 1)
annotation = annotation.strip("()")
if annotation.endswith(", optional"):
annotation = annotation[:-10]
# try to compile the annotation to transform it into an expression
annotation = parse_docstring_annotation(annotation, docstring)
else:
name = name_with_type
# try to use the annotation from the signature
try:
annotation = docstring.parent.parameters[name].annotation # type: ignore[union-attr]
except (AttributeError, KeyError):
annotation = None
try:
default = docstring.parent.parameters[name].default # type: ignore[union-attr]
except (AttributeError, KeyError):
default = None
if annotation is None:
docstring_warning(docstring, line_number, f"No type or annotation for parameter '{name}'")
if warn_unknown_params:
with suppress(AttributeError): # for parameters sections in objects without parameters
params = docstring.parent.parameters # type: ignore[union-attr]
if name not in params:
message = f"Parameter '{name}' does not appear in the function signature"
for starred_name in (f"*{name}", f"**{name}"):
if starred_name in params:
message += f". Did you mean '{starred_name}'?"
break
docstring_warning(docstring, line_number, message)
parameters.append(DocstringParameter(name=name, value=default, annotation=annotation, description=description))
return parameters, new_offset
def _read_parameters_section(
docstring: Docstring,
*,
offset: int,
**options: Any,
) -> tuple[DocstringSectionParameters | None, int]:
parameters, new_offset = _read_parameters(docstring, offset=offset, **options)
return DocstringSectionParameters(parameters), new_offset
def _read_other_parameters_section(
docstring: Docstring,
*,
offset: int,
warn_unknown_params: bool = True, # noqa: ARG001
**options: Any,
) -> tuple[DocstringSectionOtherParameters | None, int]:
parameters, new_offset = _read_parameters(docstring, offset=offset, warn_unknown_params=False, **options)
return DocstringSectionOtherParameters(parameters), new_offset
def _read_attributes_section(
docstring: Docstring,
*,
offset: int,
**options: Any,
) -> tuple[DocstringSectionAttributes | None, int]:
attributes = []
block, new_offset = _read_block_items(docstring, offset=offset, **options)
annotation: str | Expr | None = None
for line_number, attr_lines in block:
try:
name_with_type, description = attr_lines[0].split(":", 1)
except ValueError:
docstring_warning(docstring, line_number, f"Failed to get 'name: description' pair from '{attr_lines[0]}'")
continue
description = "\n".join([description.lstrip(), *attr_lines[1:]]).rstrip("\n")
if " " in name_with_type:
name, annotation = name_with_type.split(" ", 1)
annotation = annotation.strip("()")
if annotation.endswith(", optional"):
annotation = annotation[:-10]
# try to compile the annotation to transform it into an expression
annotation = parse_docstring_annotation(annotation, docstring)
else:
name = name_with_type
with suppress(AttributeError, KeyError, TypeError):
# Use subscript syntax to fetch annotation from inherited members too.
annotation = docstring.parent[name].annotation # type: ignore[index]
attributes.append(DocstringAttribute(name=name, annotation=annotation, description=description))
return DocstringSectionAttributes(attributes), new_offset
def _read_functions_section(
docstring: Docstring,
*,
offset: int,
**options: Any,
) -> tuple[DocstringSectionFunctions | None, int]:
functions = []
block, new_offset = _read_block_items(docstring, offset=offset, **options)
signature: str | Expr | None = None
for line_number, func_lines in block:
try:
name_with_signature, description = func_lines[0].split(":", 1)
except ValueError:
docstring_warning(
docstring,
line_number,
f"Failed to get 'signature: description' pair from '{func_lines[0]}'",
)
continue
description = "\n".join([description.lstrip(), *func_lines[1:]]).rstrip("\n")
if "(" in name_with_signature:
name = name_with_signature.split("(", 1)[0]
signature = name_with_signature
else:
name = name_with_signature
signature = None
functions.append(DocstringFunction(name=name, annotation=signature, description=description))
return DocstringSectionFunctions(functions), new_offset
def _read_classes_section(
docstring: Docstring,
*,
offset: int,
**options: Any,
) -> tuple[DocstringSectionClasses | None, int]:
classes = []
block, new_offset = _read_block_items(docstring, offset=offset, **options)
signature: str | Expr | None = None
for line_number, class_lines in block:
try:
name_with_signature, description = class_lines[0].split(":", 1)
except ValueError:
docstring_warning(
docstring,
line_number,
f"Failed to get 'signature: description' pair from '{class_lines[0]}'",
)
continue
description = "\n".join([description.lstrip(), *class_lines[1:]]).rstrip("\n")
if "(" in name_with_signature:
name = name_with_signature.split("(", 1)[0]
signature = name_with_signature
else:
name = name_with_signature
signature = None
classes.append(DocstringClass(name=name, annotation=signature, description=description))
return DocstringSectionClasses(classes), new_offset
def _read_modules_section(
docstring: Docstring,
*,
offset: int,
**options: Any,
) -> tuple[DocstringSectionModules | None, int]:
modules = []
block, new_offset = _read_block_items(docstring, offset=offset, **options)
for line_number, module_lines in block:
try:
name, description = module_lines[0].split(":", 1)
except ValueError:
docstring_warning(
docstring,
line_number,
f"Failed to get 'name: description' pair from '{module_lines[0]}'",
)
continue
description = "\n".join([description.lstrip(), *module_lines[1:]]).rstrip("\n")
modules.append(DocstringModule(name=name, description=description))
return DocstringSectionModules(modules), new_offset
def _read_raises_section(
docstring: Docstring,
*,
offset: int,
**options: Any,
) -> tuple[DocstringSectionRaises | None, int]:
exceptions = []
block, new_offset = _read_block_items(docstring, offset=offset, **options)
annotation: str | Expr
for line_number, exception_lines in block:
try:
annotation, description = exception_lines[0].split(":", 1)
except ValueError:
docstring_warning(
docstring,
line_number,
f"Failed to get 'exception: description' pair from '{exception_lines[0]}'",
)
else:
description = "\n".join([description.lstrip(), *exception_lines[1:]]).rstrip("\n")
# try to compile the annotation to transform it into an expression
annotation = parse_docstring_annotation(annotation, docstring)
exceptions.append(DocstringRaise(annotation=annotation, description=description))
return DocstringSectionRaises(exceptions), new_offset
def _read_warns_section(
docstring: Docstring,
*,
offset: int,
**options: Any,
) -> tuple[DocstringSectionWarns | None, int]:
warns = []
block, new_offset = _read_block_items(docstring, offset=offset, **options)
for line_number, warning_lines in block:
try:
annotation, description = warning_lines[0].split(":", 1)
except ValueError:
docstring_warning(
docstring,
line_number,
f"Failed to get 'warning: description' pair from '{warning_lines[0]}'",
)
else:
description = "\n".join([description.lstrip(), *warning_lines[1:]]).rstrip("\n")
warns.append(DocstringWarn(annotation=annotation, description=description))
return DocstringSectionWarns(warns), new_offset
def _read_block_items_maybe(
docstring: Docstring,
*,
offset: int,
multiple: bool = True,
**options: Any,
) -> _ItemsBlock:
if multiple:
return _read_block_items(docstring, offset=offset, **options)
one_block, new_offset = _read_block(docstring, offset=offset, **options)
return [(new_offset, one_block.splitlines())], new_offset
def _get_name_annotation_description(
docstring: Docstring,
line_number: int,
lines: list[str],
*,
named: bool = True,
) -> tuple[str | None, Any, str]:
if named:
match = _RE_NAME_ANNOTATION_DESCRIPTION.match(lines[0])
if not match:
docstring_warning(
docstring,
line_number,
f"Failed to get name, annotation or description from '{lines[0]}'",
)
raise ValueError
name, annotation, description = match.groups()
else:
name = None
if ":" in lines[0]:
annotation, description = lines[0].split(":", 1)
annotation = annotation.lstrip("(").rstrip(")")
else:
annotation = None
description = lines[0]
description = "\n".join([description.lstrip(), *lines[1:]]).rstrip("\n")
return name, annotation, description
def _annotation_from_parent(
docstring: Docstring,
*,
gen_index: Literal[0, 1, 2],
multiple: bool = False,
index: int = 0,
) -> str | Expr | None:
annotation = None
with suppress(Exception):
annotation = docstring.parent.annotation # type: ignore[union-attr]
if annotation.is_generator:
annotation = annotation.slice.elements[gen_index]
elif annotation.is_iterator and gen_index == 0:
annotation = annotation.slice
if multiple and annotation.is_tuple:
annotation = annotation.slice.elements[index]
return annotation
def _read_returns_section(
docstring: Docstring,
*,
offset: int,
returns_multiple_items: bool = True,
returns_named_value: bool = True,
**options: Any,
) -> tuple[DocstringSectionReturns | None, int]:
returns = []
block, new_offset = _read_block_items_maybe(
docstring,
offset=offset,
multiple=returns_multiple_items,
**options,
)
for index, (line_number, return_lines) in enumerate(block):
try:
name, annotation, description = _get_name_annotation_description(
docstring,
line_number,
return_lines,
named=returns_named_value,
)
except ValueError:
continue
if annotation:
# try to compile the annotation to transform it into an expression
annotation = parse_docstring_annotation(annotation, docstring)
else:
# try to retrieve the annotation from the docstring parent
annotation = _annotation_from_parent(docstring, gen_index=2, multiple=len(block) > 1, index=index)
if annotation is None:
returned_value = repr(name) if name else index + 1
docstring_warning(docstring, line_number, f"No type or annotation for returned value {returned_value}")
returns.append(DocstringReturn(name=name or "", annotation=annotation, description=description))
return DocstringSectionReturns(returns), new_offset
def _read_yields_section(
docstring: Docstring,
*,
offset: int,
returns_multiple_items: bool = True,
returns_named_value: bool = True,
**options: Any,
) -> tuple[DocstringSectionYields | None, int]:
yields = []
block, new_offset = _read_block_items_maybe(
docstring,
offset=offset,
multiple=returns_multiple_items,
**options,
)
for index, (line_number, yield_lines) in enumerate(block):
try:
name, annotation, description = _get_name_annotation_description(
docstring,
line_number,
yield_lines,
named=returns_named_value,
)
except ValueError:
continue
if annotation:
# try to compile the annotation to transform it into an expression
annotation = parse_docstring_annotation(annotation, docstring)
else:
# try to retrieve the annotation from the docstring parent
annotation = _annotation_from_parent(docstring, gen_index=0, multiple=len(block) > 1, index=index)
if annotation is None:
yielded_value = repr(name) if name else index + 1
docstring_warning(docstring, line_number, f"No type or annotation for yielded value {yielded_value}")
yields.append(DocstringYield(name=name or "", annotation=annotation, description=description))
return DocstringSectionYields(yields), new_offset
def _read_receives_section(
docstring: Docstring,
*,
offset: int,
receives_multiple_items: bool = True,
receives_named_value: bool = True,
**options: Any,
) -> tuple[DocstringSectionReceives | None, int]:
receives = []
block, new_offset = _read_block_items_maybe(
docstring,
offset=offset,
multiple=receives_multiple_items,
**options,
)
for index, (line_number, receive_lines) in enumerate(block):
try:
name, annotation, description = _get_name_annotation_description(
docstring,
line_number,
receive_lines,
named=receives_named_value,
)
except ValueError:
continue
if annotation:
# try to compile the annotation to transform it into an expression
annotation = parse_docstring_annotation(annotation, docstring)
else:
# try to retrieve the annotation from the docstring parent
annotation = _annotation_from_parent(docstring, gen_index=1, multiple=len(block) > 1, index=index)
if annotation is None:
received_value = repr(name) if name else index + 1
docstring_warning(docstring, line_number, f"No type or annotation for received value {received_value}")
receives.append(DocstringReceive(name=name or "", annotation=annotation, description=description))
return DocstringSectionReceives(receives), new_offset
def _read_examples_section(
docstring: Docstring,
*,
offset: int,
trim_doctest_flags: bool = True,
**options: Any,
) -> tuple[DocstringSectionExamples | None, int]:
text, new_offset = _read_block(docstring, offset=offset, **options)
sub_sections: list[tuple[Literal[DocstringSectionKind.text, DocstringSectionKind.examples], str]] = []
in_code_example = False
in_code_block = False
current_text: list[str] = []
current_example: list[str] = []
for line in text.split("\n"):
if _is_empty_line(line):
if in_code_example:
if current_example:
sub_sections.append((DocstringSectionKind.examples, "\n".join(current_example)))
current_example = []
in_code_example = False
else:
current_text.append(line)
elif in_code_example:
if trim_doctest_flags:
line = _RE_DOCTEST_FLAGS.sub("", line) # noqa: PLW2901
line = _RE_DOCTEST_BLANKLINE.sub("", line) # noqa: PLW2901
current_example.append(line)
elif line.startswith("```"):
in_code_block = not in_code_block
current_text.append(line)
elif in_code_block:
current_text.append(line)
elif line.startswith(">>>"):
if current_text:
sub_sections.append((DocstringSectionKind.text, "\n".join(current_text).rstrip("\n")))
current_text = []
in_code_example = True
if trim_doctest_flags:
line = _RE_DOCTEST_FLAGS.sub("", line) # noqa: PLW2901
current_example.append(line)
else:
current_text.append(line)
if current_text:
sub_sections.append((DocstringSectionKind.text, "\n".join(current_text).rstrip("\n")))
elif current_example:
sub_sections.append((DocstringSectionKind.examples, "\n".join(current_example)))
return DocstringSectionExamples(sub_sections), new_offset
def _read_deprecated_section(
docstring: Docstring,
*,
offset: int,
**options: Any,
) -> tuple[DocstringSectionDeprecated | None, int]:
text, new_offset = _read_block(docstring, offset=offset, **options)
# check the presence of a name and description, separated by a semi-colon
try:
version, text = text.split(":", 1)
except ValueError:
docstring_warning(docstring, new_offset, f"Could not parse version, text at line {offset}")
return None, new_offset
version = version.lstrip()
description = text.lstrip()
return (
DocstringSectionDeprecated(version=version, text=description),
new_offset,
)
def _is_empty_line(line: str) -> bool:
return not line.strip()
_section_reader = {
DocstringSectionKind.parameters: _read_parameters_section,
DocstringSectionKind.other_parameters: _read_other_parameters_section,
DocstringSectionKind.raises: _read_raises_section,
DocstringSectionKind.warns: _read_warns_section,
DocstringSectionKind.examples: _read_examples_section,
DocstringSectionKind.attributes: _read_attributes_section,
DocstringSectionKind.functions: _read_functions_section,
DocstringSectionKind.classes: _read_classes_section,
DocstringSectionKind.modules: _read_modules_section,
DocstringSectionKind.returns: _read_returns_section,
DocstringSectionKind.yields: _read_yields_section,
DocstringSectionKind.receives: _read_receives_section,
DocstringSectionKind.deprecated: _read_deprecated_section,
}
_sentinel = object()
def parse_google(
docstring: Docstring,
*,
ignore_init_summary: bool = False,
trim_doctest_flags: bool = True,
returns_multiple_items: bool = True,
warn_unknown_params: bool = True,
returns_named_value: bool = True,
returns_type_in_property_summary: bool = False,
**options: Any,
) -> list[DocstringSection]:
"""Parse a Google-style docstring.
This function iterates on lines of a docstring to build sections.
It then returns this list of sections.
Parameters:
docstring: The docstring to parse.
ignore_init_summary: Whether to ignore the summary in `__init__` methods' docstrings.
trim_doctest_flags: Whether to remove doctest flags from Python example blocks.
returns_multiple_items: Whether the `Returns` section has multiple items.
warn_unknown_params: Warn about documented parameters not appearing in the signature.
returns_named_value: Whether to parse `thing: Description` in returns sections as a name and description,
rather than a type and description. When true, type must be wrapped in parentheses: `(int): Description.`.
When false, parentheses are optional but the items cannot be named: `int: Description`.
returns_type_in_property_summary: Whether to parse the return type of properties
at the beginning of their summary: `str: Summary of the property`.
**options: Additional parsing options.
Returns:
A list of docstring sections.
"""
sections: list[DocstringSection] = []
current_section = []
in_code_block = False
lines = docstring.lines
options = {
"ignore_init_summary": ignore_init_summary,
"trim_doctest_flags": trim_doctest_flags,
"returns_multiple_items": returns_multiple_items,
"warn_unknown_params": warn_unknown_params,
"returns_named_value": returns_named_value,
"returns_type_in_property_summary": returns_type_in_property_summary,
**options,
}
ignore_summary = (
options["ignore_init_summary"]
and docstring.parent is not None
and docstring.parent.name == "__init__"
and docstring.parent.is_function
and docstring.parent.parent is not None
and docstring.parent.parent.is_class
)
offset = 2 if ignore_summary else 0
while offset < len(lines):
line_lower = lines[offset].lower()
if in_code_block:
if line_lower.lstrip(" ").startswith("```"):
in_code_block = False
current_section.append(lines[offset])
elif line_lower.lstrip(" ").startswith("```"):
in_code_block = True
current_section.append(lines[offset])
elif match := _RE_ADMONITION.match(lines[offset]):
groups = match.groupdict()
title = groups["title"]
admonition_type = groups["type"]
is_section = admonition_type.lower() in _section_kind
has_previous_line = offset > 0
blank_line_above = not has_previous_line or _is_empty_line(lines[offset - 1])
has_next_line = offset < len(lines) - 1
has_next_lines = offset < len(lines) - 2
blank_line_below = has_next_line and _is_empty_line(lines[offset + 1])
blank_lines_below = has_next_lines and _is_empty_line(lines[offset + 2])
indented_line_below = has_next_line and not blank_line_below and lines[offset + 1].startswith(" ")
indented_lines_below = has_next_lines and not blank_lines_below and lines[offset + 2].startswith(" ")
if not (indented_line_below or indented_lines_below):
# Do not warn when there are no contents,
# this is most probably not a section or admonition.
current_section.append(lines[offset])
offset += 1
continue
reasons = []
kind = "section" if is_section else "admonition"
if (indented_line_below or indented_lines_below) and not blank_line_above:
reasons.append(f"Missing blank line above {kind}")
if indented_lines_below and blank_line_below:
reasons.append(f"Extraneous blank line below {kind} title")
if reasons:
reasons_string = "; ".join(reasons)
docstring_warning(
docstring,
offset,
f"Possible {kind} skipped, reasons: {reasons_string}",
LogLevel.debug,
)
current_section.append(lines[offset])
offset += 1
continue
if is_section:
if current_section:
if any(current_section):
sections.append(DocstringSectionText("\n".join(current_section).rstrip("\n")))
current_section = []
reader = _section_reader[_section_kind[admonition_type.lower()]]
section, offset = reader(docstring, offset=offset + 1, **options) # type: ignore[operator]
if section:
section.title = title
sections.append(section)
else:
contents, offset = _read_block(docstring, offset=offset + 1)
if contents:
if current_section:
if any(current_section):
sections.append(DocstringSectionText("\n".join(current_section).rstrip("\n")))
current_section = []
if title is None:
title = admonition_type
admonition_type = admonition_type.lower().replace(" ", "-")
sections.append(DocstringSectionAdmonition(kind=admonition_type, text=contents, title=title))
else:
with suppress(IndexError):
current_section.append(lines[offset])
else:
current_section.append(lines[offset])
offset += 1
if current_section:
sections.append(DocstringSectionText("\n".join(current_section).rstrip("\n")))
if (
returns_type_in_property_summary
and sections
and docstring.parent
and docstring.parent.is_attribute
and "property" in docstring.parent.labels
):
lines = sections[0].value.lstrip().split("\n")
if ":" in lines[0]:
annotation, line = lines[0].split(":", 1)
lines = [line, *lines[1:]]
sections[0].value = "\n".join(lines)
sections.append(
DocstringSectionReturns(
[DocstringReturn("", description="", annotation=parse_docstring_annotation(annotation, docstring))],
),
)
return sections