|
| 1 | +# Documenting using Sphinx |
| 2 | + |
| 3 | +This project uses **Google style docstrings** for Python code documentation. These docstrings are compatible with Sphinx (with the `sphinx.ext.napoleon` extension) and other documentation tools. |
| 4 | + |
| 5 | +## Why Google Style Docstrings? |
| 6 | + |
| 7 | +- Clear, readable format |
| 8 | +- Widely supported by documentation generators |
| 9 | +- Easy to maintain |
| 10 | + |
| 11 | +## Example Google Style Docstring |
| 12 | + |
| 13 | +```python |
| 14 | +def add(a: int, b: int) -> int: |
| 15 | + """Add two integers. |
| 16 | +
|
| 17 | + Args: |
| 18 | + a (int): First integer. |
| 19 | + b (int): Second integer. |
| 20 | +
|
| 21 | + Returns: |
| 22 | + int: The sum of `a` and `b`. |
| 23 | +
|
| 24 | + Raises: |
| 25 | + ValueError: If either argument is not an integer. |
| 26 | + """ |
| 27 | + if not isinstance(a, int) or not isinstance(b, int): |
| 28 | + raise ValueError("Arguments must be integers.") |
| 29 | + return a + b |
| 30 | +``` |
| 31 | + |
| 32 | +## Steps to Document Your Code |
| 33 | + |
| 34 | +1. **Write Google style docstrings** for all public modules, classes, and functions. |
| 35 | +2. **Include sections** such as `Args`, `Returns`, `Raises`, and `Examples` as needed. |
| 36 | +3. **Keep docstrings concise and informative.** |
| 37 | + |
| 38 | +## Benefits of using Sphinx |
| 39 | + |
| 40 | +- Supports docstring formats – such as Google-style, NumPy-style, and reST. |
| 41 | +- Autodoc extension – can automatically extract documentation from Python docstrings. |
| 42 | +- Integration with Read the Docs – makes it easy to publish and host documentation online. |
| 43 | +- Theme support – like Read the Docs theme or the modern Furo theme. |
| 44 | + |
| 45 | +## Building the documentation |
| 46 | + |
| 47 | +```bash |
| 48 | +pip install -r docs/requirements.txt |
| 49 | +make -C docs html |
| 50 | +``` |
| 51 | + |
| 52 | +Open `docs/html/index.html` in a web browser. |
| 53 | + |
| 54 | +## References |
| 55 | + |
| 56 | +- [Google Python Style Guide](https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings) |
| 57 | +- [Sphinx Napoleon Documentation](https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html) |
0 commit comments