flake8-rst-docstrings
flake8-rst-docstrings is an MIT-licensed plugin for Flake8 that validates Python docstring markup as reStructuredText (RST). It leverages the `docutils` library and `restructuredtext-lint` internally to perform checks, ensuring that docstrings conform to RST syntax. The library is actively maintained, with its current version being 0.4.0, and development tracked on GitHub.
Common errors
-
RST301: Unexpected indentation.
cause Docstring content does not follow reStructuredText indentation rules, or an unexpected unindent occurred.fixCorrect the indentation in your docstring to adhere to reStructuredText syntax. Ensure consistent spacing and proper nesting for lists, blocks, etc. -
RST303: Unknown directive type "XXX".
cause The docstring uses a reStructuredText directive (e.g., `.. XXX::`) that is not recognized by `docutils`, often a Sphinx-specific directive not configured for the linter.fixIf using Sphinx-specific directives, add them to your `flake8` configuration file using the `rst-directives` option (e.g., `rst-directives = envvar, exception, seealso`). -
RST304: Unknown interpreted text role "XXX".
cause The docstring uses an interpreted text role (e.g., `:XXX:`) not recognized by `docutils`, typically a Sphinx-specific role.fixIf using Sphinx-specific roles, add them to your `flake8` configuration file using the `rst-roles` option (e.g., `rst-roles = class, func, ref`). -
RST213: Inline strong start-string without end-string.
cause A reStructuredText inline markup (like `**bold text**` or `*italic text*`) is missing its closing delimiter.fixEnsure all inline markup in your docstrings has matching start and end delimiters (e.g., `**strong**`, ``literal``, `*emphasis*`).
Warnings
- breaking Python 2.7 support was dropped in versions newer than 0.0.14. Python 3.8 or later is now required.
- gotcha When using Google Python Style docstrings, `flake8-rst-docstrings` may report unwanted warnings (e.g., RST307) because Google style is not strict reStructuredText until processed by tools like Sphinx's Napoleon extension. This can lead to false positives.
- gotcha Docutils, used internally for RST validation, often only provides a line number for the start of a paragraph rather than the exact line where an issue occurs. This can make pinpointing the precise location of an error more challenging.
- gotcha Some IDEs (like older VSCode Python extensions) might not properly display linter entries from `flake8` plugins using 3-character codes (like 'RST') by default. This is due to how the IDE's embedded linter might interact with external `flake8` installations.
Install
-
pip install flake8-rst-docstrings
Quickstart
import os
def example_function(param1: str, param2: int):
"""This is an example docstring.
:param param1: The first parameter.
:type param1: str
:param param2: The second parameter.
:type param2: int
:raises ValueError: If param2 is negative.
:return: A concatenated string.
:rtype: str
.. warning:: This is a reStructuredText warning.
"""
if param2 < 0:
raise ValueError("param2 cannot be negative")
return f"{param1}-{param2}"
# To run flake8, save the above code as 'example.py' and execute:
# flake8 example.py