docformatter Documentation

repository·master·Indexed 20 days ago

https://github.com/pycqa/docformatter

A tool that automatically formats Python docstrings to follow PEP 257 conventions and certain PEP 8 rules. It supports various docstring styles including Sphinx, Epytext, Google, and NumPy, and provides a compatibility mode for the black formatter. docformatter can be configured via command line arguments or configuration files such as pyproject.toml, setup.cfg, and tox.ini.

Tokens
7.5K
Snippets
20
Records
28
Agent score
63%

What's inside docformatter

  1. Understand docformatter's PEP 257 compliance goals

    master

    The primary goal of docformatter is to act as an autoformatting tool that produces PEP 257 compliant docstrings.

    It focuses on two main categories of requirements derived from PEP 257:

    1. Convention Requirements: These are the structural rules (the "shall" requirements) that ensure docstrings can be properly processed by tools like Docutils or pydocstyle. docformatter aims to satisfy all absolute convention requirements.
    2. Methodology Requirements: These relate to the actual content of the docstring. While harder to automate, docformatter aims to support these and provides arguments to allow users to toggle specific methodology requirements on or off.

    By following these conventions, docformatter ensures your docstrings are structurally sound for the Python ecosystem.

  2. How docformatter interprets PEP 257 requirements

    master

    docformatter applies specific interpretations and exceptions to PEP 257 to improve usability and maintain consistency with common styles:

    • Punctuation: One-line and summary lines can end with any of [. ! ?]. This is an exception to PEP 257 to remain consistent with Google style.
    • Capitalization: docformatter automatically capitalizes the first word of one-line and summary lines.
      • Exception: It preserves the case of the first word if it is a variable or filename.
      • Exception: It preserves the case of user-specified words (e.g., proper nouns like docformatter) via a user option.
    • Closing Quotes: For wrapped one-liners, docformatter can be configured to place closing quotes on a line by themselves, interpreting the PEP 257 requirement for multi-line docstrings as applicable to wrapped one-liners.
  3. Supported docstring styles in docformatter

    master

    docformatter supports formatting for several common docstring styles. When a specific style is selected, the tool will ignore docstrings written in other styles to avoid incorrect formatting.

    Supported styles include:

    • NumPy style
    • Google style
    • Sphinx style: Includes logic to ensure one blank space between a field name and field body, while respecting special cases like links or empty field bodies.
    • Epytext style
    • Black compatible mode: Wraps summaries and descriptions at 88 characters by default and handles specific quoting behaviors to match black formatting.
  4. Supported docstring styles (flavors)

    master

    While all supported docstring styles follow PEP 257 convention requirements, they differ in the markup syntax used for field lists (like documenting parameters and types). docformatter is designed to allow users to select their preferred "flavor" via arguments.

    The four common styles supported/addressed are:

    • Epytext: Uses @type and @param syntax.
    • Sphinx: Uses :param: and :type: reST syntax.
    • Google: Uses an Args: block.
    • NumPy: Uses a Parameters section with underlined headers.

    docformatter also considers style requirements such as line wrapping (to support tools like Emacs' fill-paragraph) and compatibility with other formatters like black (e.g., handling spaces before double quotes in one-line docstrings).

    Epytext syntax:
    @type num_dogs: int
    @param num_dogs: the number of dogs
    
    Sphinx syntax:
    :param param1: The first parameter, defaults to 1.
    :type: int
    
    Google syntax:
    Args:
        param1 (int): The first parameter.
    
    NumPy syntax:
    Parameters
    ----------
    param1 : int
        The first parameter.
  5. Understand docformatter's implementation of PEP 257

    master

    docformatter is designed to automate PEP 257 compliance. It implements most core PEP 257 conventions, including the use of triple double quotes ("""), raw strings for backslashes (r"""), and unicode strings (u""").

    It also handles specific PEP 257 requirements for:

    • One-line docstrings: Ensuring they fit on a single line, use triple quotes, and have closing quotes on the same line.
    • Multi-line docstrings: Managing summary lines, blank lines, and indentation.

    Note that some PEP 257 methodology requirements (like specific content for class, module, or function docstrings) are not enforced by docformatter, as these are semantic/content-based rather than purely stylistic.

  6. Requirements for correct docstring formatting

    master

    To ensure docformatter recognizes and correctly formats complex text patterns (like lists, parameter descriptions, and reStructuredText sections), your docstrings must strictly follow established specifications.

    If docstrings do not follow these patterns, docformatter may treat them as simple text, leading to incorrect indentation or wrapping. Supported specifications include:

    • PEP 257: Docstring Conventions
    • reStructuredText (reST): Markup Specification
    • Sphinx: Documentation Style
    • Epydoc: Documentation Style
  7. Install docformatter

    master

    You can install docformatter via pip.

    If you intend to use pyproject.toml for configuration and are using a Python version older than 3.11, you must install the [tomli] extra to provide TOML parsing support. For Python 3.11 and later, tomllib is included in the standard library and no extra is needed.

    You can also install specific release candidates or tags directly from GitHub.

    # Standard installation
    $ pip install --upgrade docformatter
    
    # Installation with tomli support (required for Python < 3.11 when using pyproject.toml)
    $ pip install --upgrade docformatter[tomli]
    
    # Install a specific release candidate or tag from GitHub
    $ pip install git+https://github.com/PyCQA/docformatter.git@<RC_TAG>
  8. Use docformatter with pre-commit

    master

    To integrate docformatter into your pre-commit workflow, add the following to your .pre-commit-config.yaml.

    Note that if you use --in-place, the commit will fail because pre-commit detects that the hook modified the files. If you use --check, the commit will fail because docformatter returns a non-zero exit code when formatting is required.

    If you are using pyproject.toml for configuration, you must include tomli in additional_dependencies.

    - repo: https://github.com/PyCQA/docformatter
      rev: v1.7.5
      hooks:
        - id: docformatter
          additional_dependencies: [tomli]
          args: [--in-place, --config, ./pyproject.toml]
  9. Configure docformatter using configuration files

    master

    You can store docformatter command line options in a configuration file to avoid repetitive CLI arguments.

    Supported Files and Precedence

    docformatter searches the current directory for supported files in the following order of precedence:

    1. pyproject.toml
    2. setup.cfg
    3. tox.ini

    You can also specify a configuration file explicitly using the --config flag.

    Configuration Syntax

    • In pyproject.toml: Use the [tool.docformatter] section.
    • In setup.cfg or tox.ini: Use the [docformatter] section.

    Options within these files should use the same names as the command line arguments.

    Precedence Rule

    Command line arguments always take precedence over settings found in a configuration file.

    # Explicitly passing a config file
    $ docformatter --config ~/.secret/path/to/pyproject.toml
    # pyproject.toml
    [tool.docformatter]
    recursive = true
    wrap-summaries = 82
    blank = true
    # setup.cfg or tox.ini
    [docformatter]
    recursive = true
    wrap-summaries = 82
    blank = true
  10. Install docformatter with TOML support

    master

    If you intend to use pyproject.toml for configuration, you must install the [tomli] extra if you are using a Python version older than 3.11. For Python 3.11 and later, docformatter uses the standard library's tomllib, so no extra installation is required.

    $ pip install --upgrade docformatter[tomli]