What is pydocstyle
masterPEP 257 guidelines out of the box, though it is not a formal reference implementation. It is compatible with Python versions 3.7 through 3.11 (Python 3.6 compatibility is attempted but not tested).repository·master·Indexed 22 days ago
https://github.com/pycqa/pydocstylepydocstyle is a static analysis tool used to check Python code for compliance with docstring conventions, specifically supporting most of PEP 257, as well as NumPy and Google style guides. It provides a CLI for checking files and directories, supports configuration via files like pyproject.toml or setup.cfg, and can be integrated into pre-commit workflows. Note that the project is officially deprecated in favor of Ruff.
PEP 257 guidelines out of the box, though it is not a formal reference implementation. It is compatible with Python versions 3.7 through 3.11 (Python 3.6 compatibility is attempted but not tested).The determination of whether a construct is public directly controls whether D1xx errors (missing docstrings) are reported:
pydocstyle will not report any D1xx errors for it or any of its children. For example, a method named bar inside a private class _Foo will not trigger a D102 error even if it lacks a docstring.pydocstyle may still report other non-D1xx errors based on your style guide requirements.The D1xx group of errors (missing docstrings in public constructs) depends on whether a construct is considered public. A construct is public only if it meets both of the following criteria:
_name or __name).Special Cases & Rules:
__init__.py).sys.path is reached. This prevents the full filesystem path from affecting publicity.__all__ Variable: If a module defines __all__ as a literal list or tuple, only the top-level constructs listed in __all__ are considered public. Note that pydocstyle does not execute the module, so it only recognizes literal definitions of __all__ and not mutations made at runtime.By default, pydocstyle merges configuration from parent directories into the local configuration.
Merge Logic:
select, ignore, or convention: If specified in the child config, the parent's settings for these keys are ignored and replaced by the child's settings. If not specified in the child, the parent's settings are inherited.add_select or add_ignore: These are additive; they add or remove error codes from the list inherited from the parent.match or match_dir: If specified in the child, they are used; otherwise, the parent's values are used.Disabling Inheritance:
To prevent pydocstyle from looking at parent directories (common for configuration files located at the repository root), add inherit = false to your configuration file.
CLI Override:
If you provide select, ignore, or convention via the Command Line Interface (CLI), the configuration file settings for these specific keys will be ignored. However, match and match-dir from the config file will still be applied.
[pydocstyle]
inherit = falseThe pydocstyle project is officially deprecated and no longer actively maintained. It is highly recommended to switch to Ruff, which offers full parity with pydocstyle, better support for the latest Python versions, and ongoing development.
Ruff Repository: https://github.com/astral-sh/ruff
You can skip pydocstyle checks for specific functions or methods by adding inline comments. This is useful for handling exceptions where a docstring does not follow the standard style rules.
# noqa.# noqa: <ERROR_CODE>,<ERROR_CODE>. This syntax is compatible with flake8 skip patterns (e.g., # noqa: D102,E501,D203).>>> def bad_function(): # noqa: D400
... """Omit a period in the docstring as an exception"""
... passInstall the pydocstyle package using pip to begin checking your Python docstrings for style compliance.
pip install pydocstyleYou can customize pydocstyle behavior within your .pre-commit-config.yaml by using the args key. This allows you to pass flags like --ignore or --select directly through the pre-commit hook.
Note that you can use either single-line or multiline YAML syntax for the arguments list.
- id: pydocstyle
args:
- --ignore=D100,D203,D405
# or multiline
- |-
--select=
D101,
D2pydocstyle automatically detects configuration files. It searches for files in the following order and continues searching up the directory tree until one is found:
setup.cfgtox.ini.pydocstyle.pydocstyle.ini.pydocstylerc.pydocstylerc.inipyproject.tomlSection Requirements:
setup.cfg, .pydocstyle), use the [pydocstyle] section.pyproject.toml), use the [tool.pydocstyle] section.Note on TOML support:
To use pyproject.toml, you must have the toml Python package installed. You can install it via the optional dependency:
pip install "pydocstyle[toml]"Deprecation Warning:
Using files named .pep257 or the [pep257] section is deprecated and will be removed in the next major version.
# Example setup.cfg
[pydocstyle]
inherit = false
ignore = D100,D203,D405
match = .*\.pyBecause pydocstyle.py is self-contained, you can run it directly from the source file without a formal installation.
python pydocstyle.pyYou can use pydocstyle as a hook in your pre-commit workflow. When configured this way, pre-commit passes checked-in Python files as positional arguments to pydocstyle, so you do not need to manually specify --match=*.py.
To add it, include the following configuration in your .pre-commit-config.yaml file, replacing |version| with a specific git hash or tag.
- repo: https://github.com/pycqa/pydocstyle
rev: |version|
hooks:
- id: pydocstyle