Generate Docstrings with type-hint information.
Project description
Docstring Generator Extension
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.
- Async-function support: Handles both
defandasync deffunctions transparently. - Type-Hint Awareness: Extracts type information from annotations and default values.
- Multiple Styles: Supports popular docstring formats:
- reST (reStructuredText)
- Google style
- NumPy style
- Format-style detection: Automatically detects the style of an existing docstring and refuses to silently mix styles unless
allow_overwrite=Trueis passed. - Style conversion: With
allow_overwrite=True, converts a docstring written in one style to another in a single call. - Exception detection: Analyses the function body with Python's
astmodule to identify raised exceptions and include them in the docstring. - Docstring coverage auditing:
check_docstring()reports how many functions in a file have complete, partial, or missing docstrings without modifying the file. - High Performance: Core logic implemented in C++ for fast processing.
- Preserves Existing Content: Keeps manually written descriptions across re-runs using special in-docstring markers:
$Nbinds the text on that line to the N-th function parameter.>>provides the return-value description.
Installation
Prerequisites
- Python 3.13 or higher
- A C++ compiler with C++20 support (e.g., GCC, Clang, or MSVC)
pybind11
Building from Source
-
Clone the repository:
git clone https://github.com/FelixTheC/docstring_generator_ext.git cd docstring_generator_ext
-
Install the
buildpackage:pip install build
-
Build the package:
python -m build
-
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)
Overwriting an existing docstring format
By default, parse_file refuses to overwrite a docstring that was already written in a different style than the one you requested, and will print a warning instead. Pass allow_overwrite=True to let the extension convert the existing docstring to the new style:
import docstring_generator_ext
file_path = "path/to/your_script.py"
style = docstring_generator_ext.DocstringFormatStyle.NUMPY
# Convert any existing docstring style to NUMPY — previous style will be removed
docstring_generator_ext.parse_file(file_path, style, allow_overwrite=True)
Auditing docstring coverage
You can 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.reSTdocstring_generator_ext.DocstringFormatStyle.GOOGLEdocstring_generator_ext.DocstringFormatStyle.NUMPY
Preserving descriptions with special markers
When the extension processes a file that already contains docstrings, it tries to keep manually written descriptions in place. Two marker conventions are supported:
$N — argument description markers
Place a $ followed by the 1-based index of the parameter inside the docstring to bind a free-form description to that argument. The marker and the text on its line are extracted and attached to the corresponding parameter; the $N line is then removed from the generated docstring.
def add(a: int, b: int) -> int:
"""Add two numbers together.
$1 The first operand.
$2 The second operand.
"""
return a + b
After the next parse_file run the descriptions will be wired to a and b automatically.
def add(a: int, b: int) -> int:
"""Add two numbers together.
Args:
a (int): The first operand.
b (int): The second operand.
Returns:
int
"""
return a + b
>> — return description marker
Place >> on its own line inside the docstring to provide the description for the return value. The text after >> on that line is extracted as the return description, and the marker line is removed.
def square(x: int) -> int:
"""Square a number.
>> The squared value of x.
"""
return x * x
After the next parse_file run the descriptions will be wired to Returns description automatically.
def square(x: int) -> int:
"""Square a number.
Args:
x (int):
Returns:
int: The squared value of x.
"""
return x * x
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file docstring_generator_ext-2.0.5.tar.gz.
File metadata
- Download URL: docstring_generator_ext-2.0.5.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2d1497c7e54778d5f64a2d49648ad1f5ee1e53b7c41f180719c006200eea8e0
|
|
| MD5 |
9b3380bc6bc96dc6521990f1982b8027
|
|
| BLAKE2b-256 |
22c7b88e96c8436063a86697dd7c5388e0674eb76ab4cd768d795e5d256b326d
|
Provenance
The following attestation bundles were made for docstring_generator_ext-2.0.5.tar.gz:
Publisher:
python-publish-ext.yml on FelixTheC/docstring_generator_ext
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
docstring_generator_ext-2.0.5.tar.gz -
Subject digest:
d2d1497c7e54778d5f64a2d49648ad1f5ee1e53b7c41f180719c006200eea8e0 - Sigstore transparency entry: 2171675725
- Sigstore integration time:
-
Permalink:
FelixTheC/docstring_generator_ext@d626cbd1c99b85ab75f7ca7769780f2fae55a122 -
Branch / Tag:
refs/tags/2.0.5 - Owner: https://github.com/FelixTheC
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish-ext.yml@d626cbd1c99b85ab75f7ca7769780f2fae55a122 -
Trigger Event:
release
-
Statement type:
File details
Details for the file docstring_generator_ext-2.0.5-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: docstring_generator_ext-2.0.5-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 250.6 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ea0a14d9e51f3d414380a87b8fe1bca9ef86ed4b98efd1f5d2928a180edc732
|
|
| MD5 |
12f134468523c4e3896409efa885db0e
|
|
| BLAKE2b-256 |
4ee814dea86df437cf68714e2e0899c1d0743cd9621b199fbd518efd64ef7ad4
|
Provenance
The following attestation bundles were made for docstring_generator_ext-2.0.5-cp313-cp313-win_amd64.whl:
Publisher:
python-publish-ext.yml on FelixTheC/docstring_generator_ext
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
docstring_generator_ext-2.0.5-cp313-cp313-win_amd64.whl -
Subject digest:
4ea0a14d9e51f3d414380a87b8fe1bca9ef86ed4b98efd1f5d2928a180edc732 - Sigstore transparency entry: 2171675841
- Sigstore integration time:
-
Permalink:
FelixTheC/docstring_generator_ext@d626cbd1c99b85ab75f7ca7769780f2fae55a122 -
Branch / Tag:
refs/tags/2.0.5 - Owner: https://github.com/FelixTheC
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish-ext.yml@d626cbd1c99b85ab75f7ca7769780f2fae55a122 -
Trigger Event:
release
-
Statement type:
File details
Details for the file docstring_generator_ext-2.0.5-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: docstring_generator_ext-2.0.5-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 646.0 kB
- Tags: CPython 3.13, manylinux: glibc 2.27+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ef1590d1baef8f873bb567a8eaa759cc3cbca24fb1dd01daa7588ca7013e32a
|
|
| MD5 |
b6d3376c84f110d7d571f938c7e48b8c
|
|
| BLAKE2b-256 |
81ee87284066c59b147724b7d8cda114bdc1f832d27bd5c7b9e109208e95d1cd
|
Provenance
The following attestation bundles were made for docstring_generator_ext-2.0.5-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:
Publisher:
python-publish-ext.yml on FelixTheC/docstring_generator_ext
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
docstring_generator_ext-2.0.5-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl -
Subject digest:
1ef1590d1baef8f873bb567a8eaa759cc3cbca24fb1dd01daa7588ca7013e32a - Sigstore transparency entry: 2171675764
- Sigstore integration time:
-
Permalink:
FelixTheC/docstring_generator_ext@d626cbd1c99b85ab75f7ca7769780f2fae55a122 -
Branch / Tag:
refs/tags/2.0.5 - Owner: https://github.com/FelixTheC
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish-ext.yml@d626cbd1c99b85ab75f7ca7769780f2fae55a122 -
Trigger Event:
release
-
Statement type:
File details
Details for the file docstring_generator_ext-2.0.5-cp313-cp313-macosx_13_0_x86_64.whl.
File metadata
- Download URL: docstring_generator_ext-2.0.5-cp313-cp313-macosx_13_0_x86_64.whl
- Upload date:
- Size: 419.2 kB
- Tags: CPython 3.13, macOS 13.0+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e58be01a58181ff0891bc3a61c0007ff58e2f12741c1e4c0544aaed20f039bf
|
|
| MD5 |
5518b8dbf8369f5561ee88f53b74c84d
|
|
| BLAKE2b-256 |
5f51d0c11e77ee5810ffaab1ccb5f6a67340faf9c8de212e54a7aabd2d6626df
|
Provenance
The following attestation bundles were made for docstring_generator_ext-2.0.5-cp313-cp313-macosx_13_0_x86_64.whl:
Publisher:
python-publish-ext.yml on FelixTheC/docstring_generator_ext
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
docstring_generator_ext-2.0.5-cp313-cp313-macosx_13_0_x86_64.whl -
Subject digest:
5e58be01a58181ff0891bc3a61c0007ff58e2f12741c1e4c0544aaed20f039bf - Sigstore transparency entry: 2171675811
- Sigstore integration time:
-
Permalink:
FelixTheC/docstring_generator_ext@d626cbd1c99b85ab75f7ca7769780f2fae55a122 -
Branch / Tag:
refs/tags/2.0.5 - Owner: https://github.com/FelixTheC
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish-ext.yml@d626cbd1c99b85ab75f7ca7769780f2fae55a122 -
Trigger Event:
release
-
Statement type:
File details
Details for the file docstring_generator_ext-2.0.5-cp313-cp313-macosx_13_0_arm64.whl.
File metadata
- Download URL: docstring_generator_ext-2.0.5-cp313-cp313-macosx_13_0_arm64.whl
- Upload date:
- Size: 420.8 kB
- Tags: CPython 3.13, macOS 13.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acec2af970a586d14e2551c98c8277874baecb5372056f3c9aa13683e29d062c
|
|
| MD5 |
b197076bfafe269bf3c88048bfb31d8f
|
|
| BLAKE2b-256 |
b9c1d9a2caae18813b75ec2b6fdca0b83b01e2ac52ec3a685f770d1ad2295e5a
|
Provenance
The following attestation bundles were made for docstring_generator_ext-2.0.5-cp313-cp313-macosx_13_0_arm64.whl:
Publisher:
python-publish-ext.yml on FelixTheC/docstring_generator_ext
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
docstring_generator_ext-2.0.5-cp313-cp313-macosx_13_0_arm64.whl -
Subject digest:
acec2af970a586d14e2551c98c8277874baecb5372056f3c9aa13683e29d062c - Sigstore transparency entry: 2171675880
- Sigstore integration time:
-
Permalink:
FelixTheC/docstring_generator_ext@d626cbd1c99b85ab75f7ca7769780f2fae55a122 -
Branch / Tag:
refs/tags/2.0.5 - Owner: https://github.com/FelixTheC
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish-ext.yml@d626cbd1c99b85ab75f7ca7769780f2fae55a122 -
Trigger Event:
release
-
Statement type: