Skip to content

Commit 1081fd5

Browse files
committed
docs: Add how-to set objects' docstring styles
1 parent a22237a commit 1081fd5

3 files changed

Lines changed: 141 additions & 0 deletions

File tree

docs/guide/users.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,12 @@ These how-tos will show you how to achieve specific things with Griffe.
112112

113113
[:octicons-arrow-right-24: See how to selectively inspect objects](users/how-to/selectively-inspect.md)
114114

115+
- :material-select:{ .lg .middle } **Set objects' docstring style**
116+
117+
---
118+
119+
Sometimes the wrong docstring styles are attached to objects. You can fix this with a few different methods.
120+
121+
[:octicons-arrow-right-24: See how to set the correct docstring styles on objects](users/how-to/set-docstring-styles.md)
122+
115123
</div>
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Setting the right docstring style for every docstring
2+
3+
Griffe attaches the specified docstring style and parsing options to each object in the tree of the package(s) you load. If your package(s) use several docstring styles, some of these objects will have the wrong style attached to them. This is problematic because other Griffe extensions rely on this attached style to parse docstrings and modify them. We plan to alleviate this limitation in the future (see [issue-340](https://github.com/mkdocstrings/griffe/issues/340)), but the most robust thing you can do is to make sure each object has the *right style* attached, as easly as possible, so that other extensions can work without issue.
4+
5+
There are currently two ways to make sure objects have the right docstring style attached as early as possible:
6+
7+
1. Use the [`auto` docstring style](https://mkdocstrings.github.io/griffe/reference/docstrings/#auto-style) (currently only available to sponsors). Griffe will use regular expressions to infer the docstring style used. 100% accuracy is impossible to achieve, so it's possible that you get incorrect styles for some objects.
8+
2. Write and use a custom Griffe extension.
9+
10+
This how-to provides a few extension-based solutions to correctly set docstring styles in your packages. **Just make sure to enable these extensions in first position.**
11+
12+
## Markup comment
13+
14+
Depending on the markup you use in docstrings, you can add a comment that tells Griffe which docstring style to use.
15+
16+
=== "Markdown"
17+
18+
```python
19+
def function():
20+
"""Summary.
21+
22+
Body.
23+
24+
<!-- style: google -->
25+
"""
26+
```
27+
28+
=== "reStructuredText"
29+
30+
```python
31+
def function():
32+
"""Summary.
33+
34+
Body.
35+
36+
.. style: google
37+
"""
38+
```
39+
40+
Your Griffe extension can then use regular expressions to search for such comments. For example with Markdown (HTML) comments:
41+
42+
```python
43+
import re
44+
import griffe
45+
46+
47+
class ApplyDocstringStyle(griffe.Extension):
48+
def __init__(self, regex: str = "<!-- style: (google|numpy|sphinx) -->") -> None:
49+
self.regex = re.compile(regex)
50+
51+
def on_instance(self, *, obj: griffe.Object, **kwargs) -> None:
52+
if obj.docstring:
53+
if match := self.regex.search(obj.docstring.value):
54+
obj.docstring.parser = match.group(1)
55+
```
56+
57+
## Python comment
58+
59+
You could also decide to add a trailing comment to your docstrings to indicate which style to use.
60+
61+
```python
62+
def function():
63+
"""Summary.
64+
65+
Body.
66+
""" # style: google
67+
```
68+
69+
Your extension can then pick up this comment to assign the right style:
70+
71+
```python
72+
import re
73+
import griffe
74+
75+
76+
class ApplyDocstringStyle(griffe.Extension):
77+
def __init__(self, regex: str = ".*# style: (google|numpy|sphinx)$") -> None:
78+
self.regex = re.compile(regex)
79+
80+
def on_instance(self, *, obj: griffe.Object, **kwargs) -> None:
81+
if obj.docstring:
82+
if match := self.regex.search(obj.docstring.source):
83+
obj.docstring.parser = match.group(1)
84+
```
85+
86+
## Explicit configuration
87+
88+
Finally, you could decide to map a list of objects to the docstring style they should use. Your extension can either accept options, or it could hard-code that list:
89+
90+
```python
91+
import griffe
92+
from fnmatch import fnmatch
93+
94+
class ApplyDocstringStyle(griffe.Extension):
95+
def __init__(self, config: dict[str, str]):
96+
self.instances = {}
97+
self.globs = {}
98+
for key, value in config.items():
99+
if "*" in key:
100+
self.globs[key] = value
101+
else:
102+
self.instances[key] = value
103+
104+
def on_instance(self, *, obj: griffe.Object, **kwargs) -> None:
105+
if obj.path in self.instances:
106+
if obj.docstring:
107+
obj.docsring.parser = self.instances[obj.path]
108+
else:
109+
for pattern, style in self.globs:
110+
if fnmatch(obj.path, pattern):
111+
if obj.docstring:
112+
obj.docstring.parser = style
113+
```
114+
115+
Example configuration in MkDocs:
116+
117+
```yaml
118+
plugins:
119+
- mkdocstrings:
120+
handlers:
121+
python:
122+
options:
123+
extensions:
124+
- your_griffe_extension.py:
125+
config:
126+
path.to.obj1: google
127+
path.to.obj2: numpy
128+
path.to.obj3.*: sphinx
129+
path.to.obj4*: google
130+
```
131+
132+
The benefit of this last solution is that it works for code you don't have control over. An alternative solution is to use the [griffe-autodocstringstyle extension](https://mkdocstrings.github.io/griffe/extensions/official/autodocstringstyle/) (sponsors only), which automatically assigns the `auto` style to all objects coming from sources found in a virtual environment.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ nav:
4949
- Parse docstrings: guide/users/how-to/parse-docstrings.md
5050
- Support custom decorators: guide/users/how-to/support-decorators.md
5151
- Selectively inspect objects: guide/users/how-to/selectively-inspect.md
52+
- Set objects' docstring style: guide/users/how-to/set-docstring-styles.md
5253
- Contributor guide:
5354
- guide/contributors.md
5455
- Environment setup: guide/contributors/setup.md

0 commit comments

Comments
 (0)