Skip to main content

Generate Docstrings with type-hint information.

Project description

Docstring Generator Extension

C++ Tests Build Extension PyPI version Python versions License: MIT

docstring_generator_ext is a high-performance Python extension written in C++ (using pybind11) designed to automatically generate and inject docstrings into Python source files. It leverages Python's ast module to extract type-hint information and function signatures to create well-formatted docstrings in various styles.

Features

  • Automatic Docstring Injection: Parses Python files and inserts docstrings for functions and methods.
  • Type-Hint Awareness: Extracts type information from annotations and default values.
  • Multiple Styles: Supports popular docstring formats:
    • reST (reStructuredText)
    • Google style
    • NumPy style
  • High Performance: Core logic implemented in C++ for fast processing.
  • Preserves Existing Content: Can update existing docstrings while trying to preserve manually added descriptions (using a special $ marker convention).

Installation

Prerequisites

  • Python 3.13 or higher
  • A C++ compiler with C++20 support (e.g., GCC, Clang, or MSVC)
  • pybind11

Building from Source

  1. Clone the repository:

    git clone https://github.com/FelixTheC/docstring_generator_ext.git
    cd docstring_generator_ext
    
  2. Install the build package:

    pip install build
    
  3. Build the package:

    python -m build
    
  4. Install the built wheel:

    pip install dist/docstring_generator_ext-*.whl
    

Usage

After installation, you can use the extension in your Python scripts:

import docstring_generator_ext

# Path to the Python file you want to process
file_path = "path/to/your_script.py"

# Choose a style: GOOGLE, NUMPY, or reST
style = docstring_generator_ext.DocstringFormatStyle.GOOGLE

# Generate and inject docstrings
docstring_generator_ext.parse_file(file_path, style)

You can also audit an existing file to see how well its functions are documented, without making any changes:

import docstring_generator_ext

# Path to the Python file you want to audit
file_path = "path/to/your_script.py"

# Returns a dict with docstring coverage statistics
result = docstring_generator_ext.check_docstring(file_path)

print(f"Functions checked  : {result['num_functions_checked']}")
print(f"Complete docstrings: {result['complete_docstrings']}")
print(f"Partial docstrings : {result['partial_docstrings']}")
print(f"No docstrings      : {result['no_docstrings']}")

The returned dictionary always contains four keys:

Key Description
num_functions_checked Total number of functions/methods found in the file
complete_docstrings Functions whose docstring fully matches the signature
partial_docstrings Functions with an incomplete or outdated docstring
no_docstrings Functions with no docstring at all

Docstring Styles

The extension provides an enum DocstringFormatStyle to choose the desired output:

  • docstring_generator_ext.DocstringFormatStyle.reST
  • docstring_generator_ext.DocstringFormatStyle.GOOGLE
  • docstring_generator_ext.DocstringFormatStyle.NUMPY

C++20

The core of this extension is written in C++20 to take full advantage of the modern standard's best algorithms and features:

  • std::format: Used for clean, type-safe string formatting throughout the docstring generation logic.
  • Ranges & views: C++20 ranges enable expressive, composable data transformations without raw loops.
  • Concepts: Improve template code clarity and provide better compiler error messages.
  • std::span: Provides safe, bounds-checked views over contiguous data without ownership overhead.

Compiler Requirements

Building from source requires a C++ compiler with full C++20 support:

Platform Minimum version
Linux GCC 11+ / Clang 14+
macOS Apple Clang 15+ / GCC 13+ (via Homebrew)
Windows MSVC 2022 (19.30+)

Pre-built wheels on PyPI are compiled with C++20 enabled and require no special toolchain on the user's side.

Authors

  • FelixTheC

License

This project is licensed under the MIT License - see the LICENSE.md file for 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

docstring_generator_ext-2.0.2.tar.gz (13.7 kB view details)

Uploaded Source

Built Distributions

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

docstring_generator_ext-2.0.2-cp313-cp313-win_amd64.whl (245.7 kB view details)

Uploaded CPython 3.13Windows x86-64

docstring_generator_ext-2.0.2-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (615.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

docstring_generator_ext-2.0.2-cp313-cp313-macosx_13_0_x86_64.whl (405.6 kB view details)

Uploaded CPython 3.13macOS 13.0+ x86-64

docstring_generator_ext-2.0.2-cp313-cp313-macosx_13_0_arm64.whl (408.9 kB view details)

Uploaded CPython 3.13macOS 13.0+ ARM64

File details

Details for the file docstring_generator_ext-2.0.2.tar.gz.

File metadata

  • Download URL: docstring_generator_ext-2.0.2.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for docstring_generator_ext-2.0.2.tar.gz
Algorithm Hash digest
SHA256 87d0e46130f8e8c8c1c7becf1ed7497af6e265d4f813f26ce7b5ab85e9921307
MD5 b4f1dfba6f03a8f749c2f40ddb98eeb8
BLAKE2b-256 3c409e86f4338530324fcd87b9b769076e4c418fa40d641df112e2f2ec1e6155

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstring_generator_ext-2.0.2.tar.gz:

Publisher: python-publish-ext.yml on FelixTheC/docstring_generator_ext

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

File details

Details for the file docstring_generator_ext-2.0.2-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for docstring_generator_ext-2.0.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 bd424746bdcb67fea84d8705496fb9ab5215db6e95f75bc00fa9fbf5aca29cec
MD5 208833ae0350df7d6223edead8efc28f
BLAKE2b-256 d0f1c16e205f35e4415edf16eee2d7c1e6c1b04bfa43d388a73ac40b2a160a83

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstring_generator_ext-2.0.2-cp313-cp313-win_amd64.whl:

Publisher: python-publish-ext.yml on FelixTheC/docstring_generator_ext

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

File details

Details for the file docstring_generator_ext-2.0.2-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for docstring_generator_ext-2.0.2-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 da410cd59522a70419ac0c9b411ec0dcbb4baa32ed2c90e9d76336f0c1e71e0e
MD5 164f3ffc1ac7d6f9bfe00ac2e2d684a1
BLAKE2b-256 343e29314492ffe59af64ccb7971e5545f76651320be9e50fbd037c0a41a609d

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstring_generator_ext-2.0.2-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: python-publish-ext.yml on FelixTheC/docstring_generator_ext

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

File details

Details for the file docstring_generator_ext-2.0.2-cp313-cp313-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for docstring_generator_ext-2.0.2-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 1633261af5bfebff631934ffa04501c5be6f2ffb9c3c95a0e6dd8c2176868263
MD5 1aca2d782160e7ce86ce771e33524b30
BLAKE2b-256 3d7b4d24414837c1d538e82300b48c8f9bfa592e34a43bb617fba89ceb79c47b

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstring_generator_ext-2.0.2-cp313-cp313-macosx_13_0_x86_64.whl:

Publisher: python-publish-ext.yml on FelixTheC/docstring_generator_ext

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

File details

Details for the file docstring_generator_ext-2.0.2-cp313-cp313-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for docstring_generator_ext-2.0.2-cp313-cp313-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 9c9bc16eddc259f28dbd1704ad08327c4d2134336e2f67abdc14cb66ceb5fad9
MD5 12d635dc2ffbf7c0142048a56bc33837
BLAKE2b-256 401aa1f50fcebeaffc51eac4c766c1c4b1c4015d0e6de10cf4a870eda3938075

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstring_generator_ext-2.0.2-cp313-cp313-macosx_13_0_arm64.whl:

Publisher: python-publish-ext.yml on FelixTheC/docstring_generator_ext

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