Skip to main content

capnp-stub-gen

Generate Python type stubs (.pyi files) from Cap'n Proto schema files.

This tool parses .capnp files using pycapnp and generates .pyi stub files that provide proper type information for Pylance/Pyright.

Features

  • Generates type stubs for Cap'n Proto structs (Reader and Builder classes)
  • Supports enums with proper type definitions
  • Handles nested structs and lists
  • Generates runtime Python modules for easy schema loading
  • CLI interface with batch processing support
  • Full type annotations for strict type checking

Requirements

  • Python 3.14+
  • pycapnp 2.0+
  • Cap'n Proto compiler (capnp) installed on your system

Installation

pip install capnp-stub-gen

Or using Poetry:

poetry add capnp-stub-gen

Usage

Command Line

Generate stubs for a single schema:

capnp-stub-gen generate path/to/schema.capnp -o output/

Generate stubs for multiple schemas:

capnp-stub-gen batch schema1.capnp schema2.capnp -o output/

Options

capnp-stub-gen generate [OPTIONS] SCHEMA

Arguments:
  SCHEMA                    Path to the .capnp schema file

Options:
  -o, --output PATH         Output directory for generated stubs [default: .]
  --proto-path TEXT         Python expression for schema path in runtime module
  --no-runtime              Skip generating the runtime .py module
  -v, --verbose             Enable verbose output
  --version                 Print version and exit
  --help                    Show this message and exit

Python API

from pathlib import Path
from capnp_stub_gen import StubGenerator

# Generate stubs
generator = StubGenerator("path/to/schema.capnp")
generator.write_files(Path("output/"))

# Or get the stub content as a string
stub_content = generator.generate_stub()
print(stub_content)

Generated Files

For a schema file myschema.capnp, the tool generates:

  • myschema.pyi - Type stub file with all type definitions
  • myschema.py - Runtime module that loads and exports the schema

Example

Given a schema:

@0xabcdef1234567890;

struct Person {
  name @0 :Text;
  age @1 :UInt32;
  email @2 :Text;
}

The generated stub includes:

class PersonReader:
    """Reader for Person Cap'n Proto struct."""

    @property
    def name(self) -> str: ...

    @property
    def age(self) -> int: ...

    @property
    def email(self) -> str: ...

class PersonBuilder:
    """Builder for Person Cap'n Proto struct."""

    @property
    def name(self) -> str: ...

    @name.setter
    def name(self, value: str) -> None: ...

    # ... etc

Development

Setup

# Clone the repository
git clone https://github.com/yourusername/capnp-stub-gen.git
cd capnp-stub-gen

# Install dependencies
poetry install --with dev

# Run tests
poetry run pytest

# Run type checking
poetry run pyright src/

# Run linting
poetry run ruff check src/ tests/

Running Tests

# All tests
poetry run pytest

# With coverage
poetry run pytest --cov=capnp_stub_gen --cov-report=html

# Specific test file
poetry run pytest tests/test_generator.py

License

MIT License. See LICENSE for details.

Contributing

Contributions are welcome. Please open an issue or submit a pull request.

Release files for capnp-stub-gen 0.1.3

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

Source distribution (sdist)

Source distribution for capnp-stub-gen 0.1.3
File Size Uploaded
capnp_stub_gen-0.1.3.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for capnp-stub-gen 0.1.3
File Interpreter ABI Platform
capnp_stub_gen-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 21.8 kB

Release files / capnp_stub_gen-0.1.3.tar.gz

Download URL capnp_stub_gen-0.1.3.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
56e7df32e55cfa3f33cc80a131b36913bfbd89f57e2d7a46951341b0b5883e04
BLAKE2b-256 checksum
How to use checksums
2977b6c1cc391917da3c31dbde4e0baeaf6347809f9b7c7838aaff92f7dbaa74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 31, 2026.

Transparency log

Release files / capnp_stub_gen-0.1.3-py3-none-any.whl

Download URL capnp_stub_gen-0.1.3-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
939bf5ea4345265adb7ac2ebd27af9d6df5a893ed5c1a37dd943456a5edd8d85
BLAKE2b-256 checksum
How to use checksums
3795c5e53f33201290e99290181ac2323a775d922c00666db9fe363324a3aa70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 31, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

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