Skip to main content

A tool to automatically format Python docstrings that tries to follow recommendations from PEP 8 and PEP 257.

Reason this release was yanked:

Incorrect version numbering

Project description

PyPI pyversions Coverage Status Tests pre-commit.ci status Documentation Status

Pydocstringformatter

A tool to automatically format Python docstrings that tries to follow recommendations from PEP 8 and PEP 257. See What it does for currently supported auto-formatting.

How to install

pip install pydocstringformatter

Usage

Click here for a full Usage overview.

usage: pydocstringformatter [-h] [-w] [--exclude EXCLUDE] [-v] [files ...]

positional arguments:
  files                 The directory or files to format.

options:
  -h, --help            show this help message and exit
  -w, --write           Write the changes to file instead of printing the diffs to stdout.
  --quiet               Do not print any logging or status messages to stdout.
  -v, --version         Show version number and exit.

configuration:
  --exclude EXCLUDE     A comma separated list of glob patterns of file path names not to be formatted.

Configuration

Pydocstringformatter will also read any configuration added to the [tool.pydocstringformatter] section of a pyproject.toml file.

For example:

[tool.pydocstringformatter]
write = True
exclude = "**/my_dir/**,**/my_other_dir/**"
# Or:
exclude = ["**/my_dir/**", "**/my_other_dir/**"]

Pre-commit

Pydocstringformatter can also be used as a pre-commit hook. Add the following to your .pre-commit-config.yaml file:

- repo: https://github.com/DanielNoord/pydocstringformatter
  rev: SPECIFY VERSION HERE
  hooks:
    - id: pydocstringformatter

What it does

The following examples show what pydocstringformatter will pick up on. All bad examples will be rewritten to follow the good patterns.

PEP 8: Note that most importantly, the """ that ends a multiline docstring should be on a line by itself:

# Bad
"""My
multi-line docstring"""

# Good
"""My
multi-line docstring
"""

PEP 257: The closing quotes are on the same line as the opening quotes

This can be enforced on multi-line docstrings with the --summary-quotes-same-line option. This behaviour is turned off by default.

# Bad
"""
My docstring"""

"""My docstring
"""

"""
My
multi-line docstring
"""

# With --summary-quotes-same-line
"""
My
multi-line docstring
"""

# Good
"""My docstring"""

"""My docstring"""

"""
My
multi-line docstring
"""

# With --summary-quotes-same-line
"""My
multi-line docstring
"""

PEP 257: The docstring is a phrase ending in a period & Multi-line docstrings consist of a summary line just like a one-line docstring

Since the first line should be a phrase or summary the first character gets capitalized.

When the second line is one recurring character we consider the summary line to be a title as used in many Sphinx documentation schemes and do not add a period.

# Bad
"""my docstring"""

"""
summary

my docstring
"""


# Good
"""My docstring."""

"""
Summary.

my docstring
"""

"""My title
===========

My docstring
"""

PEP 257: Multi-line docstrings consist of a summary line just like a one-line docstring, followed by a blank line, followed by a more elaborate description.

When the second line is one recurring character we consider the summary line to be a title as used in many Sphinx documentation schemes and do not add a white line.

# Bad
"""Summary. Body."""

"""Summary.
Body.
"""

# Good
"""Summary.

Body.
"""

"""My title
===========

My docstring
"""

PEP 257: For consistency, always use """triple double quotes""" around docstrings.

# Bad
"My docstring"

'My docstring'

'''My docstring'''

'''Summary.

Body.
'''

# Good
"""My docstring"""

"""My docstring"""

"""My docstring"""

"""Summary.

Body.
"""

Trailing or leading whitespaces get removed as well.

# Bad
"""  My docstring.  """

"""  Summary.

Body
  """

"""  My docstring.

    My indented section
"""

# Good
"""My docstring."""

"""  Summary.

Body
"""

"""My docstring.

    My indented section
"""

Development

For development and contributing guidelines please see Development.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydocstringformatter-0.5.1.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydocstringformatter-0.5.1-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

Details for the file pydocstringformatter-0.5.1.tar.gz.

File metadata

  • Download URL: pydocstringformatter-0.5.1.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for pydocstringformatter-0.5.1.tar.gz
Algorithm Hash digest
SHA256 3392c9e4425484268767ee719ee6000408625c43dc93ea3e86bee7db2218185e
MD5 2c7335efe95c23cd8ede536d12dc657d
BLAKE2b-256 604080a88c258dcb5781f92a623656962699db44c80faaeec10d750792df804a

See more details on using hashes here.

File details

Details for the file pydocstringformatter-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: pydocstringformatter-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 20.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for pydocstringformatter-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4edfcb10d032831b6ba0c83f1c94a668440608fef083f33f16648a43bc825eac
MD5 0e0b6e944de9c4298a1715b04976c766
BLAKE2b-256 36d15ff65dde4f291126b93963c04c9820e6babed675baa287f7c203d39dc1a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page