Skip to main content

A tool to analyze Python functions' docstrings and provide critiques and suggestions for improvement

Project description

Docstring Auditor

PyPI version PyPI - Python Version tests release release

Meet Docstring Auditor, your go-to solution for maintaining precise and up-to-date Python code documentation. Tired of misleading or outdated docstrings? Docstring Auditor harnesses the power of large language models to analyze and critique your docstrings, ensuring they align with your code's true purpose. Accessible to both experts and novices, Docstring Auditor is your ultimate companion for clear, concise, and informative docstrings. Say hello to better code documentation!

Motivation

Recognizing the need for a reliable tool to address the challenge of keeping code documentation in sync with evolving codebases, we developed Docstring Auditor to tackle this issue head-on. Our motivation was to create an accessible, user-friendly solution that empowers developers to maintain clear and up-to-date documentation with ease, enhancing their workflow and reducing misunderstandings.

Docstring Auditor leverages the advanced capabilities of GPT-4, a powerful language model designed to deeply understand both code and natural language. By incorporating GPT-4 into our tool, Docstring Auditor examines the docstrings in your Python code, identifying discrepancies between the documentation and the actual code implementation. The analysis covers errors, warnings, and potential improvements, providing valuable critiques and suggestions to help you keep your documentation accurate and coherent. Docstring Auditor not only ensures that technical details, such as variables and types, are consistent, but it also verifies that the docstrings' meanings are in harmony with the code's functionality.

With Docstring Auditor, you can trust that your documentation stays relevant, informative, and accessible to all members of your team, making collaboration smoother and more efficient than ever before.

Docstring Auditor can be utilized as a GitHub Action, a Docker container command-line tool, or Python package.

Features

  • Analyzes Python functions' docstrings in a given file
  • Identifies errors, warnings, and possible improvements
  • Provides detailed critiques and suggestions for better docstrings
  • Powered by OpenAI's GPT for accurate and insightful analysis
  • Easy to use command-line interface

Example of Docstring Auditor in Action

Let's say you have a Python file called example.py with the following content:

def compute(a, b):
    """
    Add two numbers.

    Parameters
    ----------
    a : int or float
        The first number to be added or from which 'b' will be subtracted.
    b : int or float
        The second number to be added or subtracted.

    Returns
    -------
    int or float
        The result of the addition operation.
    """
     if a > 0:
        return a + b
     else:
        return a - b

To analyze the docstring of the add function, run:

docstring-auditor example.py

Docstring Auditor will then provide you with a detailed analysis of the docstring, including any errors, warnings, and suggestions for improvement. The output may look like...

Processing function 1 of 1...
--------------------------------------------------------------------------------
An error was found in the function: compute

The docstring states that the function adds two numbers, but the code also performs subtraction if 'a' is less than or equal to 0. The docstring should accurately describe both addition and subtraction operations.

A warning was found in the function: compute

The docstring does not follow the numpydoc style completely. The summary line should be a one-line summary, and the extended description should be provided in a separate paragraph.

A proposed solution to these concerns is:

"""
Add or subtract two numbers based on the value of 'a'.

This function performs addition if 'a' is greater than 0, and subtraction if 'a' is less than or equal to 0.

Parameters
----------
a : int or float
    The first number to be added or from which 'b' will be subtracted.
b : int or float
    The second number to be added or subtracted.

Returns
-------
int or float
    The result of the addition or subtraction operation.
"""

GitHub Action

Docstring Auditor can be used as a GitHub Action to automatically analyze the docstrings in your Python codebase. To use Docstring Auditor as a GitHub Action, add the following to your workflow file:

  - name: Docstring Auditor
    uses: agencyenterprise/docstring-auditor@main
    with:
      openaiApiKey: ${{ secrets.OPENAI_API_KEY }}
      path: .

For an example of how to use Docstring Auditor, see this workflow.

Docker

Docstring Auditor can be used with Docker

  1. Install Docker
  2. Run the following command:
# If your code lives in the directory /Path/to/code
# And you wish to analyse all files in that directory
docker run -it --rm -e OPENAI_API_KEY=sk-XXXX -v /Path/to/code:/repo docstring-auditor
# If your code lives in the directory /Path/to/code
# And you wish to analyse a file called module/file.py
docker run -it --rm -e OPENAI_API_KEY=sk-XXXX -v /Path/to/code:/repo docstring-auditor module/file.py

Usage

If you use the docker command above, Docstring Auditor will analyse all python files in your directory. If you wish for it to analyse a single file, pass in the file name with the repo prefix. For example, to analyse the file in src/module/file.py...

docker run -it --rm -e OPENAI_API_KEY=sk-XXXX -v /Path/to/code:/repo docstring-auditor src/module/file.py

The tool will then analyze the functions' docstrings in the specified file and display the critiques and suggestions for improvement.

Local Installation

You can also run Docstring Auditor locally by following these steps:

  1. Install Python 3.6+
  2. Install Git
  3. Clone the repository: git clone git@github.com:agencyenterprise/docstring-auditor.git
  4. Setup hatch: pip install hatch
  5. Run the package hatch run docstring-auditor /path/to/your/python_file.py

Limitations

  1. There is currently no checking for malformed json from GPT4. Occasionaly, GPT4 will return a bad json and the program will crash.
  2. There is no rate limiting on the GPT4 calls.

Contributing

We welcome contributions to Docstring Auditor! If you'd like to contribute, please fork the repository and submit a pull request with your changes. We also appreciate bug reports and feature requests submitted through the GitHub issues page.

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_auditor-0.1.13.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

docstring_auditor-0.1.13-py2.py3-none-any.whl (11.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file docstring_auditor-0.1.13.tar.gz.

File metadata

  • Download URL: docstring_auditor-0.1.13.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for docstring_auditor-0.1.13.tar.gz
Algorithm Hash digest
SHA256 29cad82c2134e5c0eb798a68f00d5167f23cd552c128ed752f6674c5efcfc160
MD5 f10204dd0665680dbc87d3b15f0ae916
BLAKE2b-256 514ec5793af239c3bc499b00c95c026d60ed408953a61b7500d7956bc532fb70

See more details on using hashes here.

File details

Details for the file docstring_auditor-0.1.13-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for docstring_auditor-0.1.13-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e09d0463487bc07ae986317f17ef399af4e927f9c43cfa1110931d4f3ea2762a
MD5 d5200d0be7c7a549bbc2d25ffce115af
BLAKE2b-256 994f90a4d59602f0f4a0529fa3d8a47795c478a7a5c3a483296b13204b84d7f0

See more details on using hashes here.

Supported by

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