Skip to main content
pypi package

About

Generates stubs for python modules

There are several tweaks to target specifically modules compiled using pybind11

Package targets python3 only. (In fact, it’s compatible with python2 but it’s not officially supported)

Issues/PR are welcome

Install

From PYPI:

python -m pip install pybind11-stubgen

From github:

python -m pip install git+https://github.com/sizmailov/pybind11-stubgen.git

Usage

pybind11-stubgen [-h] [-o OUTPUT_DIR] \
                 [--root-module-suffix ROOT_MODULE_SUFFIX] \
                 [--no-setup-py] \
                 [--ignore-invalid {signature,defaultarg,all} [{signature,defaultarg,all} ...]] \
                 [--skip-signature-downgrade] \
                 [--bare-numpy-ndarray] \
                 [--log-level LOG_LEVEL] \
                 MODULE_NAME [MODULE_NAME ...]

Customization

If you need some docstring pre-processing, or you find the hard-coded pybind11-stubgen defaults to be off, you can make the adjustments using a tiny wrapper, see examples below.

Customization examples

Strip a license from docstring

import pybind11_stubgen
import re

license_pattern = re.compile(r"EXAMPLE CORP LICENCE")

def strip_license(docstring: str):
    return license_pattern.sub("", docstring)


if __name__ == '__main__':

    pybind11_stubgen.function_docstring_preprocessing_hooks.append(
        strip_license
    )

    pybind11_stubgen.main()

Replace List[int[3]] with Annotated[List[int], FixedSize(3)]

import pybind11_stubgen
import re

std_array_pattern = re.compile(r"List\[(int|complex|float)[(\d+)]]")

def std_array_fix(match: re.Match):
    return f"Annotated[List[{match.group(0)}], FixedSize({match.group(1)})]"


def strip_dimension_from_std_array(docstring: str):
    return std_array_pattern.sub(std_array_fix, docstring)


if __name__ == '__main__':

    pybind11_stubgen.function_docstring_preprocessing_hooks.append(
        strip_dimension_from_std_array
    )

    pybind11_stubgen.main()

Replace one type with another

import pybind11_stubgen
import re


def library_b_replacement(match: re.Match):
    type_name = match.group(0)
    return f"library_b.types.{type_name}"


if __name__ == '__main__':
    pybind11_stubgen.StubsGenerator.GLOBAL_CLASSNAME_REPLACEMENTS[
        re.compile(r"library_a\.types\.(\w+)")
    ] = library_b_replacement

    pybind11_stubgen.main()

Release files for pybind11-stubgen 0.16.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pybind11-stubgen 0.16.0
File Size Uploaded
pybind11-stubgen-0.16.0.tar.gz 14.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pybind11-stubgen 0.16.0
File Interpreter ABI Platform
pybind11_stubgen-0.16.0-py3-none-any.whl Python 3 none any Details

Total release size: 28.4 kB

Release files / pybind11-stubgen-0.16.0.tar.gz

Download URL pybind11-stubgen-0.16.0.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d1376e3f85f3c4bdf87a7c37a6e402f19eac306618abd723b0aca77440f8aa1e
BLAKE2b-256 checksum
How to use checksums
2dac5ed3fde1d50e13fa8698583f8e9efbf2d12021f4d5d8b69b08868684620d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release files / pybind11_stubgen-0.16.0-py3-none-any.whl

Download URL pybind11_stubgen-0.16.0-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
087f0e427405a3d2325cd1b9a1ded5de376eec17a3d07f4dca9a509d628553b0
BLAKE2b-256 checksum
How to use checksums
7c41f13e8400a95134bbc53375c2c63d68a8d3ca5a1c22117bea65df7a66d514
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release history Release notifications | RSS feed

2.5.5

2 release files

2.5.4

2 release files

2.5.3

2 release files

2.5.1

2 release files

2.5

2 release files

2.4.2

2 release files

2.4.1

2 release files

2.4

2 release files

2.3.7

2 release files

2.3.6

2 release files

2.3.5

2 release files

2.3.4

2 release files

2.3.3

2 release files

2.3.2

2 release files

2.3.1

2 release files

2.3

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2

2 release files

2.1

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

0.16.2

2 release files

0.16.1

2 release files

This release

0.16.0 This release

2 release files

0.15.1

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.10.5

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.1

2 release files

0.9

2 release files

0.8.8

2 release files

0.8.7

2 release files

0.8.6

2 release files

0.8.5

1 release file

0.8.4

1 release file

0.8.3

1 release file

0.8.2

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.3.5

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.0

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page