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.1.tar.gz (13.2 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.1-cp313-cp313-win_amd64.whl (248.2 kB view details)

Uploaded CPython 3.13Windows x86-64

docstring_generator_ext-2.0.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (612.4 kB view details)

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

docstring_generator_ext-2.0.1-cp313-cp313-macosx_13_0_x86_64.whl (399.6 kB view details)

Uploaded CPython 3.13macOS 13.0+ x86-64

docstring_generator_ext-2.0.1-cp313-cp313-macosx_13_0_arm64.whl (403.9 kB view details)

Uploaded CPython 3.13macOS 13.0+ ARM64

File details

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

File metadata

  • Download URL: docstring_generator_ext-2.0.1.tar.gz
  • Upload date:
  • Size: 13.2 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.1.tar.gz
Algorithm Hash digest
SHA256 670be9f39f6e578c218b3800e0427d308efa81ad37da6627c359644851b005db
MD5 595f06f4e75f319022a3cc1b02365f8d
BLAKE2b-256 8ea2665283b9813e92df64264660e58f08bc63c417ea1f8773b465844d7e38b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstring_generator_ext-2.0.1.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.1-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for docstring_generator_ext-2.0.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 d0a3e9c2acb7496a26cf211432a2231bbc2f08abb1ef60bdf341ad6bf3f0a69e
MD5 0ab24052ada9c19a0b12651d5bed68a8
BLAKE2b-256 4306b77d19068d2f06a60ba0f0cb7cdcb9b4b7f992e8c50c9bddb2a9702a74a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstring_generator_ext-2.0.1-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.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for docstring_generator_ext-2.0.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e67e52ab48d1584d30c4bc5256d80c2ea067dd8bd1664f8ce5c0056036f1dc77
MD5 fbcd3a105e86e671ae7dc60d86f25446
BLAKE2b-256 de9e6eb3c7f075f274d5c0f2759ecea50e2ed18747ecfc4b2e2ae50cd9bc0d3a

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstring_generator_ext-2.0.1-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.1-cp313-cp313-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for docstring_generator_ext-2.0.1-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 52082c316e8fdb0bf5d73d0fad057562977d1c97a8bc8fc92f292b26a9b012fe
MD5 2b38db9a4e3e3f7c1d0e807e8a40d33d
BLAKE2b-256 b378e81604e4922eece55835219e1f2f938d233c6eed3fdde4b53d48f2e28524

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstring_generator_ext-2.0.1-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.1-cp313-cp313-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for docstring_generator_ext-2.0.1-cp313-cp313-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 760d098e9ad56c3cc7c783cf99d9ece5d81a778af7caad00fc7dac8571227c4d
MD5 55808e3427d2fcae0c7cba906a64d070
BLAKE2b-256 885ecccf57f42ccb7fd5f76acf6acc9824e3b3c4378e16c6bbec56ee822c2eea

See more details on using hashes here.

Provenance

The following attestation bundles were made for docstring_generator_ext-2.0.1-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