Skip to main content

PEP 561 type stubs generator for cffi modules

Project description

Build Status PyPI version License Python versions

cffi-stubgen

cffi-stubgen is a tool for automatically generating PEP 561 type stubs (.pyi files) for Python modules built with cffi. This makes it easy to add type hints to your CFFI-based extensions, unlocking the full power of static type checking and IDE features.

Why generate stubs?

  • Type checking: Use tools like mypy to catch bugs before runtime.
  • IDE support: Get autocompletion, inline documentation, and better navigation in editors like VS Code and PyCharm.
  • Documentation: Stubs make your API clearer for users and contributors.

How does it work?

cffi-stubgen inspects the compiled CFFI module (not your Python source!) and generates stubs for all the functions and types exposed via CFFI. You simply point it at the built module, and it does the rest.

Quick Example

Suppose you have a CFFI module built in example_module._example. After installing your package, just run:

cffi-stubgen example_module._example

This will generate .pyi stubs for your module, ready for type checking and IDE use.

For a full walkthrough, see the example/README.md in this repository.

It will show you how cffi-stubgen can make mypy (and your IDE) aware of the arguments of

int add(int a, int b);

VSCode Autocomplete Example

Command Line Options

cffi-stubgen provides several options to customize stub generation:

  • -o, --output-dir: Set the output directory for stubs (default: same as module).
  • -t, --typedefs: Additional C types to define (space-separated).
  • --dry-run: Parse the module and report errors, but don’t write stubs.
  • --no-cleanup: If stub generation fails, do not remove incomplete stubs.
  • --verbose: Print detailed information during stub generation.
  • --stub-extension: Choose the file extension for stubs (pyi [default] or py).
  • MODULE_NAME: The fully qualified name of your cffi module (e.g., example_module._example).

Learn More

See the example for a hands-on guide!

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

cffi_stubgen-1.0.1.tar.gz (44.0 kB view details)

Uploaded Source

Built Distribution

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

cffi_stubgen-1.0.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file cffi_stubgen-1.0.1.tar.gz.

File metadata

  • Download URL: cffi_stubgen-1.0.1.tar.gz
  • Upload date:
  • Size: 44.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for cffi_stubgen-1.0.1.tar.gz
Algorithm Hash digest
SHA256 fa110dcc5f54afd849944aa8cba3bc9ef3035c457f05574ed7bc5e2dcdc007da
MD5 dbb86ae771fe3c72cb32d764f7ceaca6
BLAKE2b-256 d9bcb238ef6e7987e4424a090cb71b515cc7b601f030c6d29dc46b7e2cd77fa7

See more details on using hashes here.

File details

Details for the file cffi_stubgen-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: cffi_stubgen-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for cffi_stubgen-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c545b277d285217dd97ef4e71dfa3c2e84430692adde3ae3d919d40e69741514
MD5 fab4e46c967694ef3ff00e39a357c51c
BLAKE2b-256 12cda893cb990a27510d704b358d82ece46efd41688c188f8b47c5a53a352d52

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