Skip to main content

PEP 561 type stubs generator for pybind11 modules

Project description

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()

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

pybind11-stubgen-0.15.1.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

pybind11_stubgen-0.15.1-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file pybind11-stubgen-0.15.1.tar.gz.

File metadata

  • Download URL: pybind11-stubgen-0.15.1.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for pybind11-stubgen-0.15.1.tar.gz
Algorithm Hash digest
SHA256 f1ba34c5c3d39c4ef877a82ee0269bf33886140a42c2190eddc91ca2893a1a6c
MD5 22c018d99f7b071382e302a6682d380c
BLAKE2b-256 3987afe409bf119a36d4e305ddfe141a78978db90de398c991c95c20c550e2d0

See more details on using hashes here.

File details

Details for the file pybind11_stubgen-0.15.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pybind11_stubgen-0.15.1-py3-none-any.whl
Algorithm Hash digest
SHA256 46ad821a2acc4c5899a7fc2079a72d203c3be3791e99fbb3176a827b19902e2f
MD5 b58e1e137ee2ab6ee3a282bcdfad9926
BLAKE2b-256 c7ecfa691cb544834db82792eedcebeee55c3341a75c2a695c95612b709f03dc

See more details on using hashes here.

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