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.2b0.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.2b0-cp313-cp313-win_amd64.whl (248.2 kB view details)

Uploaded CPython 3.13Windows x86-64

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

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

docstring_generator_ext-2.0.2b0-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.2b0-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.2b0.tar.gz.

File metadata

File hashes

Hashes for docstring_generator_ext-2.0.2b0.tar.gz
Algorithm Hash digest
SHA256 3b124196eb9a87031141cda7ab0929300207196bbd5c2e2041d9e7f4a7a0c30d
MD5 03ead41da768ef2a0eaa185d5fac91c3
BLAKE2b-256 bb4d0fe18966abc519f74faa02bdfae9331f14a822873d97f058aa9421722bcb

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for docstring_generator_ext-2.0.2b0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b7b91dd6b6d34c23cc6db4e43325bea2473dce77abd594d581352f39d9e34d02
MD5 fc30f677114821bbe4bc58a336742dbc
BLAKE2b-256 db6874269596e6084db51ec759cb496aff4832df1cf533e27a5aa128b61ce40f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for docstring_generator_ext-2.0.2b0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 eab27a848f1ed331f5d4e7cca09f658306de765885cad95fedc55ba4eefffac2
MD5 aae9560dc70ec2326b0b517967f8b4c8
BLAKE2b-256 a93223a89e36b6069b11217afd11d7e3cd9e7c133aaeb27965d032e7fd2e2a76

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for docstring_generator_ext-2.0.2b0-cp313-cp313-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 49b3881ea7630fcce508bb68cafaa2de1c042a3b784f68c2421dc9618d7902c1
MD5 fd8a021d667ca8afb66961ae80d3cdab
BLAKE2b-256 e5d1556780e8f6bb7dce57ffd44d3d7b8605c3f4a02068016a8b47c0e4e37ddc

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for docstring_generator_ext-2.0.2b0-cp313-cp313-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 a97fc363f14b17d7479f1fbe7efe3c10dd68bc6f422c292aeede90a31128a7a8
MD5 46c3b5a768bac30ed3a7eea638d3484c
BLAKE2b-256 9362923b0bfa6c5c0159030ff91787df7988e8fe50bca9c4a2e4990a7949d073

See more details on using hashes here.

Provenance

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