Skip to main content

Automatic formatting of Python docstrings according to PEP 257

Project description

Docstring Tailor 🪡

Automatic formatting of Python docstrings according to PEP 257 and a predefined maximum number of chacacters per line.

Docstring conventions according to PEP 257

See https://peps.python.org/pep-0257/ for the complete document.

What is a docstring? A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object.

Different types and forms of docstrings

The PEP document makes distinctions between different types of docstrings. For some docstring properties, the convention depends on the kind of object it belongs to. For module docstrings, it can also further depend on the type of python file. Therefore, a distinction is made between these types of docstrings:

Four types of docstrings:

  1. Module docstrings
    • Module docstrings for scripts (stand-alone program)
    • Module docstrings for __init__.py files
    • Module docstrings for 'normal' modules.
  2. Class docstrings
  3. Method or function docstrings

Besides the types, docstrings can occur in two different forms.

Two forms of docstrings:

  1. One-line docstrings
  2. Multi-line docstrings

Conventions for one-line docstrings

  1. Triple quotes are used even though the string fits on one line. This makes it easy to later expand it.
  2. The closing quotes are on the same line as the opening quotes. This looks better for one-liners.
  3. There’s no blank line either before or after the docstring, except when the the type of docstring is a class docstring. Then there should be a blank line after the docstring.
  4. The docstring is a phrase ending in a period. It prescribes the function or method’s effect as a command (“Do this”, “Return that”), not as a description; e.g. don’t write “Returns the pathname …”.
  5. The one-line docstring should NOT be a “signature” reiterating the function/method parameters (which can be obtained by introspection).

Conventions for multi-line docstrings

  1. 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. The summary line may be used by automatic indexing tools; it is important that it fits on one line and is separated from the rest of the docstring by a blank line.
  2. The summary line may be on the same line as the opening quotes or on the next line.
  3. The entire docstring is indented the same as the quotes at its first line.
  4. Insert a blank line after all docstrings (one-line or multi-line) that document a class.
  5. Blank lines should be removed from the beginning and end of the docstring.

Module docstrings

  1. The docstring for a module should generally list the classes, exceptions and functions (and any other objects) that are exported by the module, with a one-line summary of each. (These summaries generally give less detail than the summary line in the object’s docstring.)
  2. The docstring of a script (a stand-alone program) should be usable as its “usage” message, printed when the script is invoked with incorrect or missing arguments (or perhaps with a “-h” option, for “help”)
  3. The docstring for a package (i.e., the docstring of the package’s __init__.py module) should also list the modules and subpackages exported by the package.

Function or method docstrings

  1. The docstring for a function or method should summarize its behavior and document its arguments, return value(s), side effects, exceptions raised, and restrictions on when it can be called (all if applicable). Optional arguments should be indicated. It should be documented whether keyword arguments are part of the interface.

Class docstrings

  1. The docstring for a class should summarize its behavior and list the public methods and instance variables. If the class is intended to be subclassed, and has an additional interface for subclasses, this interface should be listed separately (in the docstring). The class constructor should be documented in the docstring for its init method. Individual methods should be documented by their own docstring.
  2. If a class subclasses another class and its behavior is mostly inherited from that class, its docstring should mention this and summarize the differences.

Formatting operations applied by this tool

How does this tool detect docstrings?: All string literals that start with triple double quotes (""") are recognized as docstrings and will potentially be formatted.

One-line docstrings

  • Regarding the 5 conventions mentioned above for one-line docstrings, only convention number 2 is enforced (The closing quotes are on the same line as the opening quotes).

  • It is the user's reponsibility to adhere to convention number 1, since the triple quotes are used by this tool to detect the docstring. The same goes for convention number 3, 4 and 5.

  • Besides the conventions mentioned above, the docstring is formatted according to a pre-defined maximum number of characters per line (line length). This means:

    • If a docstring is spread out over multiple lines, but it could fit on one line, it will be converted to a one-line docstring.
    • If a docstring exceeds the line length, it will be converted to a multi-line docstring.

Multi-line docstrings

  • Regarding the conventions for multi-line docstrings mentioned above, number 2 is applied. This means if the summary line is on the next line, it will be enforced on the same line as the opening triple double quotes.
  • Convention number 5 is also enforced, blank lines at the start and end of the docstring are removed.
  • Most of the other conventions are about the content of the docstring, which are not checked by this tool.

Next to that, the layout of the docstring is preserved. For now the focus is on the 'Google' type docstring format, which in its most basic form, looks like this.

def function_with_pep484_type_annotations(param1: int, param2: str) -> bool:
    """Example function with PEP 484 type annotations.

    Args:
        param1: The first parameter.
        param2: The second parameter.

    Returns:
        The return value. True for success, False otherwise.
    """

Or it can look like this:

def example_generator(n):
    """Generators have a ``Yields`` section instead of a ``Returns`` section.

    Args:
        n (int): The upper limit of the range to generate, from 0 to `n` - 1.

    Yields:
        int: The next number in the range of 0 to `n` - 1.

    Examples:
        Examples should be written in doctest format, and should illustrate how
        to use the function.

        >>> print([i for i in example_generator(4)])
        [0, 1, 2, 3]

    """
    for i in range(n):
        yield i

Maintaining this structure, while enforcing a maximum characters per line, means the following rules have to be implented in the formatting logic.

  • If the user starts a new line, even though there is still space on the current line for the next word, it should be corrected and the word should be moved to the current line.
  • If the line is too long, the part that exceed the line limit should be moved to the next line.
  • If the user uses two '\n' values, it means this was a deliberate choice (for example, starting the 'Args' section) and this should be preserved.
  • Single '\n' characters in for example the 'Args' section should be preserved, as each parameter should start on a new line.
  • If the docstring contains an 'Example' or 'Examples' section, the indentation for the code part and the code itself (indicated with '>>>' and '...') should be untouched, as this code should be able to be interpreted.

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

docstring_tailor-0.1.0.tar.gz (52.1 kB view details)

Uploaded Source

Built Distribution

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

docstring_tailor-0.1.0-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file docstring_tailor-0.1.0.tar.gz.

File metadata

  • Download URL: docstring_tailor-0.1.0.tar.gz
  • Upload date:
  • Size: 52.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for docstring_tailor-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cf8ff192fa815934eebdc0aff1ff1ad1d0e3535971b8811106a36db5c8d45629
MD5 0d547ca4124c3b6bfef1eb726798b9ac
BLAKE2b-256 b62d7991a4d371c69226f182260eeba7b08a6f440fa0facd51785934db387d31

See more details on using hashes here.

File details

Details for the file docstring_tailor-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for docstring_tailor-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 29977e51b48cf73177a7584b60722abf491d062a8c45cdf5190a9ffb88e4d031
MD5 37ae9de3db5520589e9d8a96856d1cc8
BLAKE2b-256 a8cc3b6befaeb7fbb291ec8e16616cf9e2353678893881228311d7fe7b992d6c

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