Skip to main content

A pre-commit hook that uses AI to automatically generate Python docstrings.

Project description

DocuHook 🤖

[!NOTE] DocuHook is a pre-commit hook that automatically generates docstrings for your Python code using AI.

Key Features

  • Automatic Docstring Generation: Forget about writing docstrings manually. DocuHook does it for you with every commit.
  • Seamless Git Integration: Works in the background as a pre-commit hook without changing your natural workflow.
  • Support for Fast AI Models: Configured to work with the fastest models (e.g., Groq, Claude Haiku) to avoid slowing down your work.
  • Smart Code Analysis: Uses the ast module to precisely find functions that require documentation.

Installation

To get started, install the package using pip:

pip install docuhook

Usage

To enable DocuHook in your project, follow these 3 simple steps:

1. Configure pre-commit

In your .pre-commit-config.yaml file, add the following entry:

repos:
  - repo: https://github.com/bartoszborowski/docuhook
    rev: v0.1.0
    hooks:
      - id: docuhook

2. Install the hook

Run the following command to have pre-commit activate the hook in your repository:

pre-commit install

3. Set the API Key

DocuHook needs an API key to communicate with the language model. Set it as an environment variable. For example, for Groq:

export GROQ_API_KEY="sk-..."

Done! From now on, with every git commit, DocuHook will automatically analyze your code and add any missing docstrings.

Configuration

To use a different language model, you need to update the API endpoint and the request payload in the docuhook/ai_client.py file.

Specifically, you will need to modify the API_URL variable and the payload dictionary inside the generate_docstring function.

Here is the relevant code snippet from docuhook/ai_client.py:

# docuhook/ai_client.py

import os
import requests

# Ensure you have the correct environment variable for your API key
API_KEY = os.getenv("YOUR_API_KEY") 

# 1. Change this URL to your provider's API endpoint
API_URL = "https://api.groq.com/openai/v1/chat/completions"


def generate_docstring(code_to_document: str) -> str:
    headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}

    prompt_instruction = """
    Your sole task is to process the Python code below.
1. Generate a complete, Google-style docstring for the provided function.
2. Return only the new docstring as raw text
3. Don't include anything like: Here is the Google-style docstring for the provided function
"""
    # 2. Adjust the payload to match the new API's requirements
    payload = {
        "model": "llama3-8b-8192",
        "messages": [
            {
                "role": "system",
                "content": "You are an expert in writing clean Python code. Your task is to generate documentation in the Google Style Docstrings format. Documentation has to be written in English",
            },
            {
                "role": "user",
                "content": f"{prompt_instruction}:\n\n```python\n{code_to_document}\n```",
            },
        ],
        "temperature": 0.1,
    }

    response = requests.post(API_URL, headers=headers, json=payload)

    if response.status_code == 200:
        return response.json()["choices"][0]["message"]["content"]
    else:
        return f"API Error: {response.status_code} - {response.text}"

License

This project is licensed under the MIT License. See the LICENSE file for more details.

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

docuhook-0.1.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

docuhook-0.1.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file docuhook-0.1.1.tar.gz.

File metadata

  • Download URL: docuhook-0.1.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.4

File hashes

Hashes for docuhook-0.1.1.tar.gz
Algorithm Hash digest
SHA256 19e3bbc615b74f15c0662a5239c187c94085a06cca6265d78f7d938f8953ee05
MD5 f3b8616166a472f70a8699a4c4b14c5e
BLAKE2b-256 8f01a73713b255b99ea766513f403ebf9febe10911a91118d4c26f70a8492e36

See more details on using hashes here.

File details

Details for the file docuhook-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: docuhook-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.4

File hashes

Hashes for docuhook-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8e6901781476c323182675f60cd08994b69cae8189f39bab524a94b0d1b9ad48
MD5 e3780277e24739fdcfbfdb568929ea94
BLAKE2b-256 972508e98d726b918678756315707358ba71392d0b778f77442c4d4e9e069e79

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