Skip to content

Commit a265a9d

Browse files
committed
docs: Rework sections in docstrings docs
1 parent cdc9e1c commit a265a9d

File tree

1 file changed

+87
-39
lines changed

1 file changed

+87
-39
lines changed

docs/docstrings.md

+87-39
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@ even more structured data from source code.
55

66
The available parsers are:
77

8-
- `google`, to parse Google-style docstrings,
9-
see [Napoleon's documentation](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html)
10-
- `numpy`, to parse Numpydoc docstrings,
11-
see [Numpydoc's documentation](https://numpydoc.readthedocs.io/en/latest/format.html)
12-
- `sphinx`, to parse Sphinx-style docstrings,
13-
see [Sphinx's documentation](https://sphinx-rtd-tutorial.readthedocs.io/en/latest/docstrings.html)
14-
15-
## Syntax
8+
- `google`, to parse Google-style docstrings, see [Napoleon's documentation][napoleon]
9+
- `numpy`, to parse Numpydoc docstrings, see [Numpydoc's documentation][numpydoc]
10+
- `sphinx`, to parse Sphinx-style docstrings, see [Sphinx's documentation][sphinx]
1611

1712
Most of the time, the syntax specified in the aforementioned docs is supported.
1813
In some cases, the original syntax is not supported, or is supported but with subtle differences.
@@ -23,7 +18,11 @@ used in docstrings: it's retrieved as regular text.
2318
Tooling making use of Griffe can then choose to render
2419
the text as if it is Markdown, or AsciiDoc, or reStructuredText, etc..
2520

26-
### Google-style
21+
## Google-style
22+
23+
Google-style docstrings, see [Napoleon's documentation][napoleon].
24+
25+
### Syntax {#google-syntax}
2726

2827
Sections are written like this:
2928

@@ -115,7 +114,26 @@ def foo(a, b):
115114
"""
116115
```
117116

118-
#### Parser options
117+
### Admonitions {#google-admonitions}
118+
119+
When a section identifier does not match one of the [supported sections](#google-sections),
120+
the section is parsed as an "admonition" (or "callout").
121+
122+
Identifiers are case-insensitive, however singular and plural forms are distinct.
123+
For example, `Note:` is not the same as `Notes:`.
124+
125+
In particular, `Examples` is parsed as the [Examples section](#google-section-examples),
126+
while `Example` is parsed as an admonition whose kind is `example`.
127+
128+
The kind is obtained by lower-casing the identifier and replacing spaces with dashes.
129+
For example, an admonition whose identifier is `See also:` will have a kind equal to `see-also`.
130+
131+
Custom section titles are preserved in admonitions:
132+
`Tip: Check this out:` is parsed as a `tip` admonition with `Check this out:` as title.
133+
134+
It is up to any downstream documentation renderer to make use of these kinds and titles.
135+
136+
### Parser options {#google-options}
119137

120138
The parser accepts a few options:
121139

@@ -134,7 +152,11 @@ The parser accepts a few options:
134152
and should not be visible in your docs. Default: true.
135153
- `warn_unknown_params`: Warn about parameters documented in docstrings that do not appear in the signature. Default: true.
136154

137-
#### Attributes
155+
### Sections {#google-sections}
156+
157+
The following sections are supported.
158+
159+
#### Attributes {#google-section-attributes}
138160

139161
- Multiple items allowed
140162

@@ -186,7 +208,7 @@ For example, a type of `list[str]` will be parsed just as if it was an actual Py
186208
You can therefore use complex types (available in the current scope) in docstrings,
187209
for example `Optional[Union[int, Tuple[float, float]]]`.
188210

189-
#### Functions/Methods
211+
#### Functions/Methods {#google-section-functions}
190212

191213
- Multiple items allowed
192214

@@ -239,7 +261,7 @@ Functions:
239261
The signatures do not have to match the real ones:
240262
you can shorten them to only show the important parameters.
241263

242-
#### Classes
264+
#### Classes {#google-section-classes}
243265

244266
- Multiple items allowed
245267

@@ -292,7 +314,7 @@ Functions:
292314
The signatures do not have to match the real ones:
293315
you can shorten them to only show the important initialization parameters.
294316

295-
#### Modules
317+
#### Modules {#google-section-modules}
296318

297319
- Multiple items allowed
298320

@@ -315,7 +337,7 @@ Modules:
315337
"""
316338
```
317339

318-
#### Deprecated
340+
#### Deprecated {#google-section-deprecated}
319341

320342
Deprecated sections allow to document a deprecation that happened at a particular version.
321343
They can be used in every docstring.
@@ -330,7 +352,7 @@ Deprecated:
330352
foo: int = 0
331353
```
332354

333-
#### Examples
355+
#### Examples {#google-section-examples}
334356

335357
Examples sections allow to add examples of Python code without the use of markup code blocks.
336358
They are a mix of prose and interactive console snippets.
@@ -358,7 +380,7 @@ WARNING: **Not the same as *Example* sections.**
358380
*Example* (singular) sections are parsed as admonitions.
359381
Console code blocks will only be understood in *Examples* (plural) sections.
360382

361-
#### Parameters
383+
#### Parameters {#google-section-parameters}
362384

363385
- Aliases: Args, Arguments, Params
364386
- Multiple items allowed
@@ -412,7 +434,7 @@ For example, a type of `list[str]` will be parsed just as if it was an actual Py
412434
You can therefore use complex types (available in the current scope) in docstrings,
413435
for example `Optional[Union[int, Tuple[float, float]]]`.
414436

415-
#### Other Parameters
437+
#### Other Parameters {#google-section-other-parameters}
416438

417439
- Aliases: Keyword Args, Keyword Arguments, Other Args, Other Arguments, Other Params
418440
- Multiple items allowed
@@ -458,7 +480,7 @@ def foo(a, b):
458480
TIP: **Types in docstrings are resolved using the docstrings' parent scope.**
459481
See the same tip for parameters.
460482

461-
#### Raises
483+
#### Raises {#google-section-raises}
462484

463485
- Aliases: Exceptions
464486
- Multiple items allowed
@@ -486,7 +508,7 @@ You can document custom exception, using the names available in the current scop
486508
for example `my_exceptions.MyCustomException` or `MyCustomException` directly,
487509
depending on what you imported/defined in the current module.
488510

489-
#### Warns
511+
#### Warns {#google-section-warns}
490512

491513
- Aliases: Warnings
492514
- Multiple items allowed
@@ -513,7 +535,7 @@ You can document custom warnings, using the names available in the current scope
513535
for example `my_warnings.MyCustomWarning` or `MyCustomWarning` directly,
514536
depending on what you imported/defined in the current module.
515537

516-
#### Yields
538+
#### Yields {#google-section-yields}
517539

518540
- Multiple items allowed
519541

@@ -573,7 +595,7 @@ Yields:
573595
TIP: **Types in docstrings are resolved using the docstrings' parent scope.**
574596
See previous tips for types in docstrings.
575597

576-
#### Receives
598+
#### Receives {#google-section-receives}
577599

578600
- Multiple items allowed
579601

@@ -655,7 +677,7 @@ Receives:
655677
TIP: **Types in docstrings are resolved using the docstrings' parent scope.**
656678
See previous tips for types in docstrings.
657679

658-
#### Returns
680+
#### Returns {#google-section-returns}
659681

660682
- Multiple items allowed
661683

@@ -739,7 +761,11 @@ Setting it to false will disallow specifying a name.
739761
TIP: **Types in docstrings are resolved using the docstrings' function scope.**
740762
See previous tips for types in docstrings.
741763

742-
### Numpydoc-style
764+
## Numpydoc-style
765+
766+
Numpydoc docstrings, see [Numpydoc's documentation][numpydoc]
767+
768+
### Syntax {#numpydoc-syntax}
743769

744770
Sections are written like this:
745771

@@ -821,7 +847,22 @@ several syntaxes are supported:
821847
"""
822848
```
823849

824-
#### Parser options
850+
### Admonitions {#numpydoc-admonitions}
851+
852+
When a section identifier does not match one of the [supported sections](#numpydoc-sections),
853+
the section is parsed as an "admonition" (or "callout").
854+
855+
Identifiers are case-insensitive, however singular and plural forms are distinct,
856+
except for notes and warnings.
857+
In particular, `Examples` is parsed as the [Examples section](#numpydoc-section-examples),
858+
while `Example` is parsed as an admonition whose kind is `example`.
859+
860+
The kind is obtained by lower-casing the identifier and replacing spaces with dashes.
861+
For example, an admonition whose identifer is `See also` will have a kind equal to `see-also`.
862+
863+
It is up to any downstream documentation renderer to make use of these kinds.
864+
865+
### Parser options {#numpydoc-options}
825866

826867
The parser accepts a few options:
827868

@@ -833,7 +874,11 @@ The parser accepts a few options:
833874
and should not be visible in your docs. Default: true.
834875
- `warn_unknown_params`: Warn about parameters documented in docstrings that do not appear in the signature. Default: true.
835876

836-
#### Attributes
877+
### Sections {#numpydoc-sections}
878+
879+
The following sections are supported.
880+
881+
#### Attributes {#numpydoc-section-attributes}
837882

838883
- Multiple items allowed
839884

@@ -894,7 +939,7 @@ For example, a type of `list[str]` will be parsed just as if it was an actual Py
894939
You can therefore use complex types (available in the current scope) in docstrings,
895940
for example `Optional[Union[int, Tuple[float, float]]]`.
896941

897-
#### Functions/Methods
942+
#### Functions/Methods {#numpydoc-section-functions}
898943

899944
- Multiple items allowed
900945

@@ -956,7 +1001,7 @@ bar(baz=1)
9561001
The signatures do not have to match the real ones:
9571002
you can shorten them to only show the important parameters.
9581003

959-
#### Classes
1004+
#### Classes {#numpydoc-section-classes}
9601005

9611006
- Multiple items allowed
9621007

@@ -1018,7 +1063,7 @@ Bar(baz=1)
10181063
The signatures do not have to match the real ones:
10191064
you can shorten them to only show the important initialization parameters.
10201065

1021-
#### Modules
1066+
#### Modules {#numpydoc-section-modules}
10221067

10231068
- Multiple items allowed
10241069

@@ -1044,7 +1089,7 @@ bar
10441089
"""
10451090
```
10461091

1047-
#### Deprecated
1092+
#### Deprecated {#numpydoc-section-deprecated}
10481093

10491094
Deprecated sections allow to document a deprecation that happened at a particular version.
10501095
They can be used in every docstring.
@@ -1061,7 +1106,7 @@ Deprecated
10611106
foo: int = 0
10621107
```
10631108

1064-
#### Examples
1109+
#### Examples {#numpydoc-section-examples}
10651110

10661111
Examples sections allow to add examples of Python code without the use of markup code blocks.
10671112
They are a mix of prose and interactive console snippets.
@@ -1086,7 +1131,7 @@ Blank lines delimit prose vs. console blocks.
10861131
"""
10871132
```
10881133

1089-
#### Parameters
1134+
#### Parameters {#numpydoc-section-parameters}
10901135

10911136
- Aliases: Args, Arguments, Params
10921137
- Multiple items allowed
@@ -1149,7 +1194,7 @@ For example, a type of `list[str]` will be parsed just as if it was an actual Py
11491194
You can therefore use complex types (available in the current scope) in docstrings,
11501195
for example `Optional[Union[int, Tuple[float, float]]]`.
11511196

1152-
#### Other Parameters
1197+
#### Other Parameters {#numpydoc-section-other-parameters}
11531198

11541199
- Aliases: Keyword Args, Keyword Arguments, Other Args, Other Arguments, Other Params
11551200
- Multiple items allowed
@@ -1181,7 +1226,7 @@ def foo(a, b, **kwargs):
11811226
TIP: **Types in docstrings are resolved using the docstrings' parent scope.**
11821227
See the same tip for parameters.
11831228

1184-
#### Raises
1229+
#### Raises {#numpydoc-section-raises}
11851230

11861231
- Aliases: Exceptions
11871232
- Multiple items allowed
@@ -1213,9 +1258,8 @@ You can document custom exception, using the names available in the current scop
12131258
for example `my_exceptions.MyCustomException` or `MyCustomException` directly,
12141259
depending on what you imported/defined in the current module.
12151260

1216-
#### Warns
1261+
#### Warns {#numpydoc-section-warns}
12171262

1218-
- Aliases: Warnings
12191263
- Multiple items allowed
12201264

12211265
Warns sections allow to document warnings emitted by the following code.
@@ -1242,7 +1286,7 @@ You can document custom warnings, using the names available in the current scope
12421286
for example `my_warnings.MyCustomWarning` or `MyCustomWarning` directly,
12431287
depending on what you imported/defined in the current module.
12441288

1245-
#### Yields
1289+
#### Yields {#numpydoc-section-yields}
12461290

12471291
- Multiple items allowed
12481292

@@ -1310,7 +1354,7 @@ t : int
13101354
TIP: **Types in docstrings are resolved using the docstrings' parent scope.**
13111355
See previous tips for types in docstrings.
13121356

1313-
#### Receives
1357+
#### Receives {#numpydoc-section-receives}
13141358

13151359
- Multiple items allowed
13161360

@@ -1401,7 +1445,7 @@ flag : int
14011445
TIP: **Types in docstrings are resolved using the docstrings' parent scope.**
14021446
See previous tips for types in docstrings.
14031447

1404-
#### Returns
1448+
#### Returns {#numpydoc-section-returns}
14051449

14061450
- Multiple items allowed
14071451

@@ -1568,3 +1612,7 @@ Returns | ✅ | ✅ | [❌][issue-xref-sphinx-returns]
15681612
[merge_init]: https://mkdocstrings.github.io/python/usage/configuration/docstrings/#merge_init_into_class
15691613
[doctest flags]: https://docs.python.org/3/library/doctest.html#option-flags
15701614
[doctest]: https://docs.python.org/3/library/doctest.html#module-doctest
1615+
1616+
[napoleon]: https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
1617+
[numpydoc]: https://numpydoc.readthedocs.io/en/latest/format.html
1618+
[sphinx]: https://sphinx-rtd-tutorial.readthedocs.io/en/latest/docstrings.html

0 commit comments

Comments
 (0)