Registry / testing / docformatter

docformatter

JSON →
library1.7.7pypypiunverified

docformatter is a Python tool that automatically formats docstrings to adhere to PEP 257 conventions, ensuring consistent and readable documentation. It handles various aspects like summary line wrapping, blank lines, and multi-line docstring structure. The library is actively maintained, with frequent releases addressing bug fixes and new features, and its current version is 1.7.7.

pip install docformatter
INSTALL
IMPORT
SIG · DOCFORMATTER
D
docformatter
testingpythonv1.7.7
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

This quickstart demonstrates how to use `docformatter` programmatically via `subprocess` to format a Python file's docstrings. It creates a temporary file, runs `docformatter --in-place` on it, and then prints the formatted content before cleaning up.

import subprocess import os # Create a dummy Python file with unformatted docstrings code_to_format = ''' def my_function(arg1, arg2): """This is a very long and unformatted summary line that definitely exceeds the default wrap length of 79 characters. This is the description. It also needs to be wrapped. It contains details about arg1 and arg2. :param arg1: The first argument. :type arg1: int :param arg2: The second argument. :type arg2: str """ pass class MyClass: """A class with a simple docstring. It needs to be formatted.""" def __init__(self): pass ''' file_path = "example_docstrings.py" with open(file_path, "w") as f: f.write(code_to_format) print(f"Original file '{file_path}':\n---\n{code_to_format}---\n") # Run docformatter to format the file in-place try: # Using --recursive (or -r) is good practice for multiple files/directories # Using --in-place (or -i) makes changes directly to the file result = subprocess.run(["docformatter", "--in-place", file_path], check=True, capture_output=True, text=True) if result.stdout: print(f"docformatter output (stdout):\n{result.stdout}") if result.stderr: print(f"docformatter errors (stderr):\n{result.stderr}") # Read the formatted content with open(file_path, "r") as f: formatted_code = f.read() print(f"Formatted file '{file_path}':\n---\n{formatted_code}---\n") except subprocess.CalledProcessError as e: print(f"Error running docformatter: {e}") print(f"stdout: {e.stdout}") print(f"stderr: {e.stderr}") finally: # Clean up the dummy file if os.path.exists(file_path): os.remove(file_path) print(f"Cleaned up '{file_path}'.")
docformatter --version
Debug
Known issues
breakingStarting with version 1.7.7, `docformatter` dropped official support for Python versions older than 3.9. Users on older Python environments will need to use a previous version of the library.
fix
Upgrade to Python 3.9 or newer, or pin `docformatter` to `<1.7.7` in your `requirements.txt`.
affects: >=1.7.7
gotchaIntegrating `docformatter` with complex docstrings, especially those using reStructuredText (reST) or Sphinx-style field lists, can sometimes lead to unexpected formatting or require specific options. The project has had several fixes related to handling Sphinx fields, directives, and wrapping.
fix
Ensure you are on the latest `docformatter` version. Utilize the `--style` option (e.g., `--style sphinx`) if using a specific docstring style. Consider `--force-wrap` for challenging cases, though it might occasionally produce less-than-ideal output for complex lists. Review the formatted output carefully.
affects: <1.7.7
gotchaWhen used as a `pre-commit` hook, incorrect `rev` or `args` in `.pre-commit-config.yaml` can lead to manifest issues or unexpected behavior. Specifically, older examples might use outdated `rev` values, or miss crucial arguments like `--in-place`.
fix
Always refer to the latest `docformatter` documentation or its GitHub repository's `.pre-commit-config.yaml` for the recommended `rev` and `args`. Ensure you include `--in-place` if you want the hook to modify files directly, or `--check` if you only want it to report issues without changing files. Run `pre-commit autoupdate` regularly.
affects: All versions
gotcha`docformatter`'s `--black` option (introduced in v1.7.0) aims for Black compatibility but may not cover all Black-specific formatting nuances or interactions, as Black primarily focuses on code formatting, not docstrings.
fix
Upgrade to `docformatter` version 1.7.0 or newer to use the `--black` option for improved compatibility. Always run `docformatter` before `black` in your formatting pipeline if both are used.
affects: <1.7.0
Errors
Common errors & fixes
CalledProcessError: command: ('/path/to/python', '-mpip', 'install', '.') return code: 1
Installation of docformatter fails on newer Python versions (e.g., 3.14) due to incompatible dependencies like 'untokenize' or 'lib2to3'.
fix
Upgrade docformatter to a version compatible with your Python interpreter, or ensure that Python version is compatible with docformatter's dependencies. For Python 3.14 and untokenize, a docformatter release that drops or vendors 'untokenize' is needed.
AttributeError: module 'docformatter' has no attribute 'format_code'
This error occurs when older code or an integration attempts to call a deprecated or removed API function 'format_code' from docformatter.
fix
Update the integrating code or script to use the current docformatter API, such as invoking it via its command-line interface or using the updated programmatic API if available. This function was removed in docformatter version 1.5.
docformatter seems to ignore the configuration in pyproject.toml and uses its default settings instead
When run as a pre-commit hook, docformatter might not automatically load configurations from `pyproject.toml`, especially in older versions or without specifying 'tomli' as an additional dependency for Python versions prior to 3.11.
fix
Explicitly pass the `--config` argument to docformatter in your `.pre-commit-config.yaml` and add `tomli` to `additional_dependencies` for Python < 3.11. Example: `args: [--in-place, --config, pyproject.toml]`
AttributeError: 'NoneType' object has no attribute 'encoding' (from_path(filename).best().encoding)
This bug occurs when docformatter's internal file encoding detection (using `from_path(filename).best()`) returns `None` for certain files, leading to an AttributeError when it tries to access the 'encoding' attribute.
fix
Upgrade docformatter to a version where this bug has been addressed and fixed (e.g., versions released after the fix for issue #277, which was closed in #304).
Upgrade
Version history
1.7.7latest on PyPI · released May 11, 2025
Audit
Dependencies
tomlioptionalRequired for parsing pyproject.toml configuration on Python versions older than 3.11. For Python 3.11+ `tomllib` from the standard library is used.
Agent activity
9 hits · last 30 days
node
8
Resources
docformatter — pip install docformatter · libregistry