Skip to main content

Flag missing docstrings and, optionally, generate them from signatures and type annotations.

Project description

Docstringify

Flag missing docstrings and, optionally, generate them from signatures and type annotations.

Last Release PyPI release Supported Python Versions License
Build status CI
Downloads PyPI downloads

About

Given a file, test.py, with the following contents:

def say_hello(name: str = 'World') -> None:
    print(f'Hello, {name}!')

You can use Docstringify in three modes:

  1. check – Flag missing docstrings:

    test is missing a docstring
    test.say_hello is missing a docstring
    
  2. suggest – Suggest docstring templates based on type annotations:

    test is missing a docstring
    Hint:
    """__description__"""
    
    test.say_hello is missing a docstring
    Hint:
    """
    __description__
    
    Parameters
    ----------
    name : str, default="World"
        __description__
    """
    
  3. edit – Add docstring templates to source code files:

    """__description__"""
    
    def say_hello(name: str = 'World') -> None:
        """
        __description__
    
        Parameters
        ----------
        name : str, default="World"
            __description__
        """
        print(f'Hello, {name}!')
    

Usage

Pre-commit hook

[!NOTE] The examples in this section apply to versions 2.0.0 and greater. If you are using an older version, consult the README at that tag.

Check mode: docstringify-check

Add the following to your .pre-commit-config.yaml file to block commits with missing docstrings before any formatters like ruff:

- repo: https://github.com/stefmolin/docstringify
  rev: <version>
  hooks:
    - id: docstringify-check

By default, all docstrings are required. If you want to be more lenient, you can set the threshold, which is the percentage of docstrings that must be present:

- repo: https://github.com/stefmolin/docstringify
  rev: <version>
  hooks:
    - id: docstringify-check
      args: [--threshold=0.75]

Suggest mode: docstringify-suggest

If you would like to see suggested docstring templates (inferred from type annotations for functions and methods), use the suggest mode, along with the docstring style you want to use (options are google, numpydoc, and stub). Here, we ask for stub suggestions (just single lines of """__description__"""):

- repo: https://github.com/stefmolin/docstringify
  rev: <version>
  hooks:
    - id: docstringify-suggest
      args: [--style=stub]

Edit mode: docstringify-edit

Use the edit mode to create a copy of each file with docstring templates. Here, we ask for changes using the Google docstring style:

- repo: https://github.com/stefmolin/docstringify
  rev: <version>
  hooks:
    - id: docstringify-edit
      args: [--style=google]

If you want the changes to be made in place, add --overwrite. Here, we ask for numpydoc-style docstring suggestions:

- repo: https://github.com/stefmolin/docstringify
  rev: <version>
  hooks:
    - id: docstringify-edit
      args: [--overwrite, --style=numpydoc]

[!WARNING] Make sure you only operate on files that are in version control if you are using --overwrite.

Be sure to check out the pre-commit documentation for additional configuration options.

Command line

First, install the docstringify package from PyPI:

$ python -m pip install docstringify

Then, use the docstringify entry point on the file(s) of your choice. For example, to run in check mode:

$ docstringify check /path/to/file [/path/to/another/file]

Run docstringify --help for more information.

Python

First, install the docstringify package from PyPI:

$ python -m pip install docstringify

Then, use the DocstringVisitor() class on individual files to see spots where docstrings are missing:

>>> from docstringify.traversal import DocstringVisitor
>>> visitor = DocstringVisitor('test.py')
>>> visitor.process_file()
test is missing a docstring
test.say_hello is missing a docstring

If you would like to see suggested docstring templates (inferred from type annotations for functions and methods), provide a converter:

>>> from docstringify.converters import NumpydocDocstringConverter
>>> from docstringify.traversal import DocstringVisitor
>>> visitor = DocstringVisitor('test.py', converter=NumpydocDocstringConverter)
>>> visitor.process_file()
test is missing a docstring
Hint:
"""__description__"""

test.say_hello is missing a docstring
Hint:
"""
__description__

Parameters
----------
name : str, default="World"
    __description__
"""

To make changes to your files, you will need to use the DocstringTransformer instead. With the DocstringTransformer, the converter is required:

>>> from docstringify.converters import GoogleDocstringConverter
>>> from docstringify.traversal import DocstringTransformer
>>> transformer = DocstringTransformer('test.py', converter=GoogleDocstringConverter)
>>> transformer.process_file()
test is missing a docstring
test.say_hello is missing a docstring
Docstring templates written to /.../test_docstringify.py

If you want to overwrite the file with the edits, pass overwrite=True to DocstringTransformer():

>>> from docstringify.converters import GoogleDocstringConverter
>>> from docstringify.traversal import DocstringTransformer
>>> transformer = DocstringTransformer(
...     'test.py', converter=GoogleDocstringConverter, overwrite=True
... )
>>> transformer.process_file()
test is missing a docstring
test.say_hello is missing a docstring
Docstring templates written to /.../test.py

Contributing

Please consult the contributing guidelines.

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

docstringify-2.0.0.tar.gz (40.7 kB view details)

Uploaded Source

Built Distribution

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

docstringify-2.0.0-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

Details for the file docstringify-2.0.0.tar.gz.

File metadata

  • Download URL: docstringify-2.0.0.tar.gz
  • Upload date:
  • Size: 40.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for docstringify-2.0.0.tar.gz
Algorithm Hash digest
SHA256 8b7743f02ae1bc8a8d382daa1c3e364496e96dc2776b20887f09d6a3cfdee6c8
MD5 ad129d546c2254ebfed275f77b7205b1
BLAKE2b-256 907d556365285771e12d52d7d4f8d4a0812c472bf5b41eb96bb808f45d6853dd

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstringify-2.0.0.tar.gz:

Publisher: pypi-publish.yml on stefmolin/docstringify

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file docstringify-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: docstringify-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 24.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for docstringify-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5672eb54ab085bbfcab26d224b13f1239ff846fa1eb3551eb7336217730bdedc
MD5 09c7651e75ba72f565ba8e35144f30ed
BLAKE2b-256 3b7f25ec0f5f65d175236d02f13c8cf45e945155b64b8075bafa2f0f8f4eb519

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstringify-2.0.0-py3-none-any.whl:

Publisher: pypi-publish.yml on stefmolin/docstringify

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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