Generate Python type stubs from Cap'n Proto schema files
Project description
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 definitionsmyschema.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.
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 Distribution
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 capnp_stub_gen-0.1.1.tar.gz.
File metadata
- Download URL: capnp_stub_gen-0.1.1.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7be20a2518310121eb9d18575d49e6a69268ca915a8413b58688027cad176577
|
|
| MD5 |
5b3c215459cd8741c8b5482ca649eefb
|
|
| BLAKE2b-256 |
62c4d293c6c3f96028bde5d38cefef763f06ae4162823bdb1505fcadf2873540
|
Provenance
The following attestation bundles were made for capnp_stub_gen-0.1.1.tar.gz:
Publisher:
publish.yaml on KaiErikNiermann/capnp-stub-gen
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
capnp_stub_gen-0.1.1.tar.gz -
Subject digest:
7be20a2518310121eb9d18575d49e6a69268ca915a8413b58688027cad176577 - Sigstore transparency entry: 843161054
- Sigstore integration time:
-
Permalink:
KaiErikNiermann/capnp-stub-gen@0b5bd009cddd4ce723d26968c8c927bc4339a2d5 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/KaiErikNiermann
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@0b5bd009cddd4ce723d26968c8c927bc4339a2d5 -
Trigger Event:
release
-
Statement type:
File details
Details for the file capnp_stub_gen-0.1.1-py3-none-any.whl.
File metadata
- Download URL: capnp_stub_gen-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23cee238e5a155b0ae382be8ddf0ed0de9f4e7bbfc2ab697b4a3b949590a1ac1
|
|
| MD5 |
8b47da4ba32cac2c91882d478ac3648a
|
|
| BLAKE2b-256 |
1260dd849ec584bbbc7f9747cb91cfc74b9487604c64f9390750d54b0d927721
|
Provenance
The following attestation bundles were made for capnp_stub_gen-0.1.1-py3-none-any.whl:
Publisher:
publish.yaml on KaiErikNiermann/capnp-stub-gen
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
capnp_stub_gen-0.1.1-py3-none-any.whl -
Subject digest:
23cee238e5a155b0ae382be8ddf0ed0de9f4e7bbfc2ab697b4a3b949590a1ac1 - Sigstore transparency entry: 843161062
- Sigstore integration time:
-
Permalink:
KaiErikNiermann/capnp-stub-gen@0b5bd009cddd4ce723d26968c8c927bc4339a2d5 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/KaiErikNiermann
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@0b5bd009cddd4ce723d26968c8c927bc4339a2d5 -
Trigger Event:
release
-
Statement type: