| 1 |
# Doctest Formatting |
| 2 |
|
| 3 |
While _Black_ makes some decisions about styling for docstrings, it does not make any |
| 4 |
assumptions about the contents of documentation. Thus, executable Python code inside |
| 5 |
docstrings or documentation files (e.g. doctests), will not be formatted by _Black_. |
| 6 |
|
| 7 |
Listed below are tools that apply _Black_ formatting to code inside docstrings and |
| 8 |
documentation files. |
| 9 |
|
| 10 |
> Note: There are some observed inconsistencies between the below packages. Because of |
| 11 |
> this, we hesitate to make any recommendations. Any installed packages are installed at |
| 12 |
> your own risk. We also encourage anyone to contribute documentation for additional |
| 13 |
> packages that apply _Black_ formatting to doctests. |
| 14 |
|
| 15 |
## blacken-docs |
| 16 |
|
| 17 |
[`blacken-docs`](https://pypi.org/project/blacken-docs/) is primarily used to apply |
| 18 |
_Black_ formatting to code in documentation files (e.g. `.rst`, `.md`, `.tex`). |
| 19 |
|
| 20 |
`blacken-docs` supports the following: |
| 21 |
|
| 22 |
- Python code blocks in Markdown, reStructuredText, and LaTeX files. Similar to |
| 23 |
`blackdoc`, normal _Black_ formatting is applied, so doctests inside Python code |
| 24 |
blocks will not be formatted. |
| 25 |
|
| 26 |
````md |
| 27 |
```python |
| 28 |
print("Hello world!") |
| 29 |
``` |
| 30 |
```` |
| 31 |
|
| 32 |
```rst |
| 33 |
.. code-block:: python |
| 34 |
print("Hello world!") |
| 35 |
``` |
| 36 |
|
| 37 |
```latex |
| 38 |
\begin{minted}{python} |
| 39 |
print("Hello world!") |
| 40 |
\end{minted} |
| 41 |
``` |
| 42 |
|
| 43 |
- Doctests inside Pycon code blocks in Markdown and reStructuredText. The code blocks |
| 44 |
may be included in a `.md` or `.rst` file, or inside a docstring in a Python file. |
| 45 |
|
| 46 |
````md |
| 47 |
```python |
| 48 |
>>> print("Hello world!") |
| 49 |
``` |
| 50 |
```` |
| 51 |
|
| 52 |
```rst |
| 53 |
.. code-block:: pycon |
| 54 |
>>> print("Hello world!") |
| 55 |
``` |
| 56 |
|
| 57 |
````python |
| 58 |
def add_one(n: int) -> int: |
| 59 |
""" |
| 60 |
Examples |
| 61 |
-------- |
| 62 |
```pycon |
| 63 |
>>> add_one(1) == 2 |
| 64 |
``` |
| 65 |
""" |
| 66 |
return n + 1 |
| 67 |
```` |
| 68 |
|
| 69 |
## blackdoc |
| 70 |
|
| 71 |
[`blackdoc`](https://pypi.org/project/blackdoc/) is primarily used to apply _Black_ |
| 72 |
formatting to doctests in Python files. It will not format any file contents that are |
| 73 |
otherwise covered by _Black_. |
| 74 |
|
| 75 |
`blackdoc` supports the following: |
| 76 |
|
| 77 |
- Doctests in Python files. |
| 78 |
|
| 79 |
```python |
| 80 |
def add_one(n: int) -> int: |
| 81 |
""" |
| 82 |
Examples |
| 83 |
-------- |
| 84 |
>>> add_one(1) == 2 |
| 85 |
""" |
| 86 |
return n + 1 |
| 87 |
``` |
| 88 |
|
| 89 |
- Python code blocks in Markdown or reStructuredText files. In these cases, normal |
| 90 |
_Black_ formatting is applied, i.e., doctests inside Python code blocks will not be |
| 91 |
formatted. |
| 92 |
|
| 93 |
````md |
| 94 |
```python |
| 95 |
print("Hello world!") |
| 96 |
``` |
| 97 |
```` |
| 98 |
|
| 99 |
```rst |
| 100 |
.. code-block:: python |
| 101 |
print("Hello world!") |
| 102 |
``` |