|
| 1 | +## Handler options |
| 2 | + |
| 3 | +Like every handler, the Python handler accepts the common |
| 4 | +[`selection`](#selection) and [`rendering`](#rendering) options, |
| 5 | +both as **global** and **local** options. |
| 6 | +The `selection` options gives you control over the selection of Python objects, |
| 7 | +while the `rendering` options lets you change how the documentation is rendered. |
| 8 | + |
| 9 | +It also accepts these additional **global-only** options: |
| 10 | + |
| 11 | +Option | Type | Description | Default |
| 12 | +------ | ---- | ----------- | ------- |
| 13 | +**`setup_commands`** | `list of str` | Run these commands before starting the documentation collection. | `[]` |
| 14 | + |
| 15 | +!!! example "Example: setup Django before collecting documentation" |
| 16 | + ```yaml |
| 17 | + # mkdocs.yml |
| 18 | + plugins: |
| 19 | + - mkdocstrings: |
| 20 | + handlers: |
| 21 | + python: |
| 22 | + setup_commands: |
| 23 | + - import os |
| 24 | + - import django |
| 25 | + - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_django_app.settings") |
| 26 | + - django.setup() |
| 27 | + ``` |
| 28 | + |
| 29 | +!!! important |
| 30 | + Additional options like `setup_commands` are used only once, |
| 31 | + when instantiating the handler the first time it is requested. |
| 32 | + This is why they are considered global-only options, |
| 33 | + as they will have no effect if used as local options. |
| 34 | + |
| 35 | +### Selection |
| 36 | + |
| 37 | +The following options are directly passed to the handler's collector. |
| 38 | +See [Collector: pytkdocs](#collector-pytkdocs) to learn more about `pytkdocs`. |
| 39 | + |
| 40 | +Option | Type | Description | Default |
| 41 | +------ | ---- | ----------- | ------- |
| 42 | +**`filters`** | `list of str` | List of filtering regular expressions. Prefix with `!` to exclude objects whose name match. The default means *exclude private members*. | `["!^_[^_]"]` |
| 43 | +**`members`** | `bool`, or `list of str` | Explicitly select members. True means *all*, false means *none*. | `True` |
| 44 | +**`inherited_members`** | `bool` | Also select members inherited from parent classes. | `False` |
| 45 | +**`docstring_style`** | `str` | Docstring style to parse. `pytkdocs` supports `google`, `numpy` and `restructured-text`. *Note: Numpy-style requires the `numpy-style` extra of `pytkdocs`.* | `"google"` |
| 46 | +**`docstring_options`** | `dict` | Options to pass to the docstring parser. See [Collector: pytkdocs](#collector-pytkdocs) | `{}` |
| 47 | +**`new_path_syntax`** | `bool` | Whether to use the new "colon" path syntax when importing objects. | `False` |
| 48 | + |
| 49 | +!!! example "Configuration example" |
| 50 | + === "Global" |
| 51 | + ```yaml |
| 52 | + # mkdocs.yml |
| 53 | + plugins: |
| 54 | + - mkdocstrings: |
| 55 | + handlers: |
| 56 | + python: |
| 57 | + selection: |
| 58 | + filters: |
| 59 | + - "!^_" # exlude all members starting with _ |
| 60 | + - "^__init__$" # but always include __init__ modules and methods |
| 61 | + ``` |
| 62 | + |
| 63 | + === "Local" |
| 64 | + ```yaml |
| 65 | + ::: my_package |
| 66 | + selection: |
| 67 | + filters: [] # pick up everything |
| 68 | + ``` |
| 69 | + |
| 70 | +### Rendering |
| 71 | + |
| 72 | +::: mkdocstrings.handlers.python:PythonRenderer.default_config |
| 73 | + rendering: |
| 74 | + show_root_toc_entry: false |
| 75 | + |
| 76 | +These options affect how the documentation is rendered. |
| 77 | + |
| 78 | +!!! example "Configuration example" |
| 79 | + === "Global" |
| 80 | + ```yaml |
| 81 | + # mkdocs.yml |
| 82 | + plugins: |
| 83 | + - mkdocstrings: |
| 84 | + handlers: |
| 85 | + python: |
| 86 | + rendering: |
| 87 | + show_root_heading: yes |
| 88 | + ``` |
| 89 | + |
| 90 | + === "Local" |
| 91 | + ```md |
| 92 | + ## `ClassA` |
| 93 | + |
| 94 | + ::: my_package.my_module.ClassA |
| 95 | + rendering: |
| 96 | + show_root_heading: no |
| 97 | + heading_level: 3 |
| 98 | + ``` |
| 99 | + |
| 100 | +## Collector: pytkdocs |
| 101 | + |
| 102 | +The tool used by the Python handler to collect documentation from Python source code |
| 103 | +is [`pytkdocs`](https://pawamoy.github.io/pytkdocs). |
| 104 | +It stands for *(Python) Take Docs*, and is supposed to be a pun on MkDocs (*Make Docs*?). |
| 105 | + |
| 106 | +### Supported docstrings styles |
| 107 | + |
| 108 | +Right now, `pytkdocs` supports the Google-style, Numpy-style and reStructuredText-style docstring formats. |
| 109 | +The style used by default is the Google-style. |
| 110 | +You can configure what style you want to use with |
| 111 | +the `docstring_style` and `docstring_options` [selection options](#selection), |
| 112 | +both globally or per autodoc instruction. |
| 113 | + |
| 114 | +#### Google-style |
| 115 | + |
| 116 | +You can see examples of Google-style docstrings |
| 117 | +in [Napoleon's documentation](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html). |
| 118 | + |
| 119 | +##### Sections |
| 120 | + |
| 121 | +Docstrings sections are parsed by `pytkdocs` and rendered by *mkdocstrings*. |
| 122 | +Supported sections are: |
| 123 | + |
| 124 | +- `Arguments` (or `Args`, `Parameters`, `Params`) |
| 125 | +- `Attributes` |
| 126 | +- `Examples` (or `Example`) |
| 127 | +- `Raises` (or `Raise`, `Except`, `Exceptions`) |
| 128 | +- `Returns` (or `Return`) |
| 129 | + |
| 130 | +##### Admonitions |
| 131 | + |
| 132 | +Additionally, any section that is not recognized will be transformed into its admonition equivalent. |
| 133 | +For example: |
| 134 | + |
| 135 | +=== "Original" |
| 136 | + ```python |
| 137 | + """ |
| 138 | + Note: You can disable this behavior with the `replace_admonitions` option. |
| 139 | + To prevent `pytkdocs` from converting sections to admonitions, |
| 140 | + use the `replace_admonitions`: |
| 141 | + |
| 142 | + ```md |
| 143 | + ::: my_package.my_module |
| 144 | + selection: |
| 145 | + docstring_style: google # this is the default |
| 146 | + docstring_options: |
| 147 | + replace_admonitions: no |
| 148 | + ``` |
| 149 | + |
| 150 | + So meta! |
| 151 | + """ |
| 152 | + ``` |
| 153 | + |
| 154 | +=== "Modified" |
| 155 | + ```python |
| 156 | + """ |
| 157 | + !!! note "You can disable this behavior with the `replace_admonitions` option." |
| 158 | + To prevent `pytkdocs` from converting sections to admonitions, |
| 159 | + use the `replace_admonitions`: |
| 160 | + |
| 161 | + ```md |
| 162 | + ::: my_package.my_module |
| 163 | + selection: |
| 164 | + docstring_style: google # this is the default |
| 165 | + docstring_options: |
| 166 | + replace_admonitions: no |
| 167 | + ``` |
| 168 | + |
| 169 | + So meta! |
| 170 | + """ |
| 171 | + ``` |
| 172 | + |
| 173 | +=== "Result" |
| 174 | + !!! note "You can disable this behavior with the `replace_admonitions` parser option." |
| 175 | + To prevent `pytkdocs` from converting sections to admonitions, |
| 176 | + use the `replace_admonitions` parser option: |
| 177 | + |
| 178 | + ```md |
| 179 | + ::: my_package.my_module |
| 180 | + selection: |
| 181 | + docstring_style: google # this is the default |
| 182 | + docstring_options: |
| 183 | + replace_admonitions: no |
| 184 | + ``` |
| 185 | + |
| 186 | + So meta! |
| 187 | + |
| 188 | +As shown in the above example, this can be disabled |
| 189 | +with the `replace_admonitions` option of the Google-style parser: |
| 190 | + |
| 191 | +```yaml |
| 192 | +::: my_package.my_module |
| 193 | + selection: |
| 194 | + docstring_style: google # this is the default |
| 195 | + docstring_options: |
| 196 | + replace_admonitions: no |
| 197 | +``` |
| 198 | +
|
| 199 | +##### Annotations |
| 200 | +
|
| 201 | +Type annotations are read both in the code and in the docstrings. |
| 202 | +
|
| 203 | +!!! example "Example with a function" |
| 204 | + **Expand the source at the end to see the original code!** |
| 205 | + |
| 206 | + ::: snippets.function_annotations_google:my_function |
| 207 | + rendering: |
| 208 | + show_root_heading: no |
| 209 | + show_root_toc_entry: no |
| 210 | +
|
| 211 | +#### Numpy-style |
| 212 | +
|
| 213 | +!!! important "Extra dependency required" |
| 214 | + You'll need an extra dependency to parse Numpy-style docstrings: |
| 215 | +
|
| 216 | + ``` |
| 217 | + pdm add -d --group docs 'pytkdocs[numpy-style]' |
| 218 | + poetry add -D 'pytkdocs[numpy-style]' |
| 219 | + pip install 'pytkdocs[numpy-style]' |
| 220 | + # etc. |
| 221 | + ``` |
| 222 | + |
| 223 | +You can see examples of Numpy-style docstrings |
| 224 | +in [numpydoc's documentation](https://numpydoc.readthedocs.io/en/latest/format.html). |
| 225 | + |
| 226 | +#### reStructuredText-style |
| 227 | + |
| 228 | +!!! warning "Partial support" |
| 229 | + Only RST-**style** is supported, not the whole RST markup specification. |
| 230 | + Docstrings will still be converted as Markdown. |
| 231 | + |
| 232 | +You can see examples of reStructuredText-style docstrings |
| 233 | +in [Sphinx's documentation](https://sphinx-rtd-tutorial.readthedocs.io/en/latest/docstrings.html). |
| 234 | + |
| 235 | +##### Sections |
| 236 | + |
| 237 | +Docstrings directives are parsed by `pytkdocs` and rendered by *mkdocstrings*. |
| 238 | +Supported directives are: |
| 239 | + |
| 240 | +- `param` (or `parameter`, `arg`, `argument`, `key`, `keyword`) |
| 241 | +- `type` |
| 242 | +- `raises` (or `raise`, `except`, `exception`) |
| 243 | +- `var` (or `ivar`, `cvar`) |
| 244 | +- `vartype` |
| 245 | +- `returns` (or `return1`) |
| 246 | +- `rtype` |
| 247 | + |
| 248 | +Details about how to use each directive can be found in the |
| 249 | +[Sphinx domain documentation](https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html?highlight=python%20domain#info-field-lists) |
| 250 | + |
| 251 | +##### Annotations |
| 252 | + |
| 253 | +Type annotations are read both in the code and in the docstrings. |
| 254 | + |
| 255 | +!!! example "Example with a function" |
| 256 | + **Expand the source at the end to see the original code!** |
| 257 | + |
| 258 | + ::: snippets.function_annotations_rst:my_function |
| 259 | + selection: |
| 260 | + docstring_style: "restructured-text" |
| 261 | + rendering: |
| 262 | + show_root_heading: no |
| 263 | + show_root_toc_entry: no |
| 264 | + |
| 265 | +## Finding modules |
| 266 | + |
| 267 | +In order for `pytkdocs` to find your packages and modules, |
| 268 | +you should take advantage of the usual Python loading mechanisms: |
| 269 | + |
| 270 | +- install your package in the current virtualenv: |
| 271 | + ```bash |
| 272 | + . venv/bin/activate |
| 273 | + pip install -e . |
| 274 | + ``` |
| 275 | + |
| 276 | + ```bash |
| 277 | + poetry install |
| 278 | + ``` |
| 279 | + |
| 280 | + ...etc. |
| 281 | + |
| 282 | +- or add your package(s) parent directory in the `PYTHONPATH`. |
| 283 | + |
| 284 | +(*The following instructions assume your Python package is in the `src` directory.*) |
| 285 | + |
| 286 | +In Bash and other shells, you can run your command like this |
| 287 | +(note the prepended `PYTHONPATH=...`): |
| 288 | + |
| 289 | +```bash |
| 290 | +PYTHONPATH=src poetry run mkdocs serve |
| 291 | +``` |
| 292 | + |
| 293 | +You could also export that variable, |
| 294 | +but this is **not recommended** as it could affect other Python processes: |
| 295 | + |
| 296 | +```bash |
| 297 | +export PYTHONPATH=src # Linux/Bash and similar |
| 298 | +setx PYTHONPATH src # Windows, USE AT YOUR OWN RISKS |
| 299 | +``` |
| 300 | + |
| 301 | +You can also use the Python handler `setup_commands`: |
| 302 | + |
| 303 | +```yaml |
| 304 | +# mkdocs.yml |
| 305 | +plugins: |
| 306 | + - mkdocstrings: |
| 307 | + handlers: |
| 308 | + python: |
| 309 | + setup_commands: |
| 310 | + - import sys |
| 311 | + - sys.path.append("src") |
| 312 | + # or sys.path.insert(0, "src") |
| 313 | +``` |
| 314 | + |
| 315 | +## Mocking libraries |
| 316 | + |
| 317 | +You may want to to generate documentation for a package while its dependencies are not available. |
| 318 | +The Python handler provides itself no builtin way to mock libraries, |
| 319 | +but you can use the `setup_commands` to mock them manually: |
| 320 | + |
| 321 | +!!! example "mkdocs.yml" |
| 322 | + ```yaml |
| 323 | + plugins: |
| 324 | + - mkdocstrings: |
| 325 | + handlers: |
| 326 | + python: |
| 327 | + setup_commands: |
| 328 | + - import sys |
| 329 | + - from unittest.mock import MagicMock as mock |
| 330 | + - sys.modules["lib1"] = mock() |
| 331 | + - sys.modules["lib2"] = mock() |
| 332 | + - sys.modules["lib2.module1"] = mock() |
| 333 | + - sys.modules["lib2.module1.moduleB"] = mock() |
| 334 | + # etc |
| 335 | + ``` |
| 336 | + |
| 337 | +## Recommended style (Material) |
| 338 | + |
| 339 | +Here are some CSS rules for the |
| 340 | +[*Material for MkDocs*](https://squidfunk.github.io/mkdocs-material/) theme: |
| 341 | + |
| 342 | +```css |
| 343 | +/* Indentation. */ |
| 344 | +div.doc-contents:not(.first) { |
| 345 | + padding-left: 25px; |
| 346 | + border-left: 4px solid rgba(230, 230, 230); |
| 347 | + margin-bottom: 80px; |
| 348 | +} |
| 349 | +``` |
| 350 | + |
| 351 | +## Recommended style (ReadTheDocs) |
| 352 | + |
| 353 | +Here are some CSS rules for the built-in *ReadTheDocs* theme: |
| 354 | + |
| 355 | +```css |
| 356 | +/* Indentation. */ |
| 357 | +div.doc-contents:not(.first) { |
| 358 | + padding-left: 25px; |
| 359 | + border-left: 4px solid rgba(230, 230, 230); |
| 360 | + margin-bottom: 60px; |
| 361 | +} |
| 362 | +``` |
0 commit comments