Files
capnpc-py/README.md
T

124 lines
3.0 KiB
Markdown
Raw Normal View History

2026-07-09 15:07:34 +08:00
# capnpc-py
Cap'n Proto Python type stub generator — a `capnp compile` plugin.
Generates `.pyi` type stub files for Cap'n Proto schemas, providing IDE autocompletion and static type checking support via Pyright/Pylance.
## Requirements
- **Python** >= 3.10
- **pycapnp** >= 2.0.0 (Python package, installed automatically)
- **Cap'n Proto** >= 1.0 (system package, must be installed separately)
### Installing Cap'n Proto
| Platform | Command |
|----------|---------|
| Arch Linux | `sudo pacman -S capnproto` |
| Ubuntu/Debian | `sudo apt install capnproto` |
| macOS | `brew install capnp` |
2026-07-09 15:39:46 +08:00
| Windows | Download from [capnproto.org](https://capnproto.org/install.html) |
2026-07-09 15:07:34 +08:00
## Installation
```bash
pip install capnpc-py
```
## Usage
```bash
capnp compile -opy myschema.capnp
```
This generates `myschema_capnp.pyi` — a Python type stub file that type checkers can use. Runtime import (`import myschema_capnp`) is handled automatically by pycapnp's import hook.
2026-07-09 15:39:46 +08:00
### Multi-file projects
2026-07-09 15:07:34 +08:00
2026-07-09 15:39:46 +08:00
```bash
capnp compile -opy --src-prefix=. src/schema.capnp
```
Cross-file type references automatically produce relative imports in the generated stubs.
## Example
Input (`addressbook.capnp`):
2026-07-09 15:07:34 +08:00
```capnp
struct Person {
name @0 :Text;
age @1 :Int32;
2026-07-09 15:39:46 +08:00
phones @2 :List(PhoneNumber);
2026-07-09 15:07:34 +08:00
2026-07-09 15:39:46 +08:00
struct PhoneNumber {
number @0 :Text;
type @1 :Type;
enum Type { mobile @0; home @1; work @2; }
}
}
2026-07-09 15:07:34 +08:00
```
2026-07-09 15:39:46 +08:00
Output (`addressbook_capnp.pyi`):
2026-07-09 15:07:34 +08:00
```python
2026-07-09 15:39:46 +08:00
"""Auto-generated type stub for addressbook.capnp"""
2026-07-09 15:07:34 +08:00
from __future__ import annotations
from collections.abc import Iterator, Sequence
from contextlib import contextmanager
from typing import IO, Literal
class Person:
name: str
age: int
2026-07-09 15:39:46 +08:00
phones: Sequence[Person.PhoneNumber | Person.PhoneNumberBuilder | Person.PhoneNumberReader]
class PhoneNumber:
Type = Literal["mobile", "home", "work"]
type: Type
number: str
...
2026-07-09 15:07:34 +08:00
@staticmethod
def new_message(**kwargs) -> PersonBuilder: ...
def to_dict(self) -> dict: ...
class PersonReader(Person):
def as_builder(self) -> PersonBuilder: ...
class PersonBuilder(Person):
def to_bytes(self) -> bytes: ...
def as_reader(self) -> PersonReader: ...
2026-07-09 15:39:46 +08:00
...
2026-07-09 15:07:34 +08:00
```
2026-07-09 15:39:46 +08:00
## Supported Features
| Feature | Status |
|---------|--------|
| Structs (Reader / Builder classes) | ✅ |
| Enums (`Literal[...]` aliases) | ✅ |
| Nested types | ✅ |
| Lists (`Sequence[T]`) | ✅ |
| Unions (`which()` / `@overload init()`) | ✅ |
| Groups | ✅ |
| Self-referencing structs | ✅ |
| Constants (primitives, enums) | ✅ |
| Generics (`TypeVar`, `Generic[T]`, brand resolution) | ✅ |
| Interfaces (RPC: Client / Server classes) | ✅ |
| Cross-file imports (relative `.` imports) | ✅ |
| Annotations (`$Cxx.name`, etc.) | ⏳ |
2026-07-09 15:07:34 +08:00
## Development
Uses [PDM](https://pdm-project.org/) for project management.
```bash
2026-07-09 15:39:46 +08:00
git clone https://github.com/NCBM/capnpc-py
2026-07-09 15:07:34 +08:00
cd capnp-py
pdm install # installs dependencies + dev tools
pdm run pytest # run tests
```
## License
MIT