Skip to main content

FastProto is fast and efficient protobuf library for Python, built on the top of Rust.

Project description

FastProto

Fast, Pythonic Protocol Buffers — messages are plain, readable @dataclass types, with all encoding and decoding handled by a compiled Rust core.

Google's Python protobuf generates opaque classes full of getters/setters and a reflection API you have to learn. FastProto instead generates clean dataclasses you can construct, compare, and repr() like any other — and does the wire work in Rust.

  • Idiomatic messages — generated code is a @dataclass with plain annotations (str, int, list[...], dict[...], | None); autocomplete, type checkers, and repr() all just work.
  • Rust wire codec — encode/decode run in Rust via PyO3, not pure Python.
  • Wire-compatible — bytes interoperate both ways with Google's reference protobuf runtime.
  • Standard toolchain — ships a protoc plugin; just add --fastproto_out.

Install

pip install fastproto            # runtime (Python 3.12+)
pip install "fastproto[plugin]"  # + the protoc code generator

Code generation also needs the protoc compiler itself — install it from your package manager (brew install protobuf, apt install protobuf-compiler) or the official releases.

Quick start

1. Define user.proto:

syntax = "proto3";
package example;

enum Role {
  ROLE_UNSPECIFIED = 0;
  ROLE_ADMIN = 1;
  ROLE_USER = 2;
}

message Address {
  string city = 1;
  string street = 2;
}

message User {
  int64 id = 1;
  string name = 2;
  optional string email = 3;
  Role role = 4;
  repeated string tags = 5;
  Address address = 6;
  map<string, int32> counters = 7;
}

2. Generate with protoc:

protoc --proto_path=. --fastproto_out=. user.proto

This writes user_pb.py — a plain, readable dataclass module:

# @generated by fastproto. DO NOT EDIT.
# source: user.proto
# pyright: reportUnknownVariableType=false
from dataclasses import dataclass, field
from enum import IntEnum

from fastproto import Message, Scalar, message


class Role(IntEnum):
    ROLE_UNSPECIFIED = 0
    ROLE_ADMIN = 1
    ROLE_USER = 2


@message(_USER_DESCRIPTOR)
@dataclass(slots=True)
class User(Message):
    id: Scalar.Int64 = 0
    name: Scalar.String = ""
    email: Scalar.String | None = None
    role: Role = Role(0)
    tags: list[Scalar.String] = field(default_factory=list)
    address: "Address | None" = None
    counters: dict[Scalar.String, Scalar.Int32] = field(default_factory=dict)

3. Use it like any dataclass:

from user_pb import Address, Role, User

user = User(
    id=42,
    name="Ada",
    role=Role.ROLE_ADMIN,
    tags=["vip", "beta"],
    address=Address(city="London", street="Baker St"),
    counters={"logins": 7},
)

data = user.to_bytes()               # serialize to protobuf wire bytes
assert User.from_bytes(data) == user  # and back

No SerializeToString() / ParseFromString() ceremony and no reflection — just to_bytes() / from_bytes() on a dataclass you can build, compare, and print.

Type mapping

Each proto scalar maps to an alias under fastproto.Scalar. An alias is just the underlying Python type (int, str, ...) tagged with Annotated[...], so it type-checks as the base type while still recording the exact wire type.

proto Python proto Python
double Scalar.Double fixed32 Scalar.Fixed32
float Scalar.Float fixed64 Scalar.Fixed64
int32 Scalar.Int32 sfixed32 Scalar.SFixed32
int64 Scalar.Int64 sfixed64 Scalar.SFixed64
uint32 Scalar.UInt32 bool Scalar.Bool
uint64 Scalar.UInt64 string Scalar.String
sint32 Scalar.SInt32 bytes Scalar.Bytes
sint64 Scalar.SInt64

Composite fields: repeated Tlist[T], map<K, V>dict[K, V], enumIntEnum, and optional / message / oneof fields → T | None.

Semantics

  • Presence (proto3): plain scalars use their zero value and are not nullable; optional scalars, message fields, and oneof members are T | None and track explicit presence (a set empty string is distinct from unset). An all-default message encodes to b"".
  • oneof: members are plain optional fields; setting more than one raises ValueError at encode time.
  • References: sibling, self, and enum references resolve lazily on the first to_bytes() / from_bytes() — nothing for you to wire up.
empty = User()
assert empty.to_bytes() == b"" and empty.email is None
User(phone="1", telegram="a").to_bytes()  # ValueError: ... oneof ...

Contributing

See CONTRIBUTING.md for setup, project layout, and the release flow.

License

MIT

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

fastproto-0.1.3.tar.gz (67.5 kB view details)

Uploaded Source

Built Distributions

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

fastproto-0.1.3-cp312-abi3-win_amd64.whl (166.9 kB view details)

Uploaded CPython 3.12+Windows x86-64

fastproto-0.1.3-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (309.7 kB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ x86-64

fastproto-0.1.3-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (306.5 kB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

fastproto-0.1.3-cp312-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (532.0 kB view details)

Uploaded CPython 3.12+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file fastproto-0.1.3.tar.gz.

File metadata

  • Download URL: fastproto-0.1.3.tar.gz
  • Upload date:
  • Size: 67.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastproto-0.1.3.tar.gz
Algorithm Hash digest
SHA256 6cb38e902a7a888e780fccbf0fca0e5cd6380b5aef71ea26a6ac6ae25a16cabd
MD5 93bf20fe68ed5f8f40af4247056c55ca
BLAKE2b-256 adfad379ba9c643c411c67059b96c90c78a6667cc233ab74b287740742d9cd82

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastproto-0.1.3.tar.gz:

Publisher: release.yml on pkozhem/fastproto

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastproto-0.1.3-cp312-abi3-win_amd64.whl.

File metadata

  • Download URL: fastproto-0.1.3-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 166.9 kB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastproto-0.1.3-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 388ca0116c92bae42734f3d61506f70a9768533104e61d4b5fc1b931b6debe55
MD5 31649ba14204a4b923a25667a182a401
BLAKE2b-256 3a1b99172aff4a10c2099065b1cebf53fd858678ac01c3f3081e19033059b8d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastproto-0.1.3-cp312-abi3-win_amd64.whl:

Publisher: release.yml on pkozhem/fastproto

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastproto-0.1.3-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for fastproto-0.1.3-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b7be1ea268c5b8cacd70309c888b190e71f67fd5006813e003d71eb76dee8a57
MD5 36721f3578c122eb76f03a790e485817
BLAKE2b-256 1756cd25b065634743a4f1638a3e5b971c779e60848520ec0fd69b55d25c3808

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastproto-0.1.3-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on pkozhem/fastproto

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastproto-0.1.3-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for fastproto-0.1.3-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a422c21a250a3bca1d5995be150ca17201986e52a24a6beb73a915b61553a273
MD5 aad91999009bea6c178272d6bde18dd5
BLAKE2b-256 dae7b0312fab449bb3a24509d140963be86311618a36ab3db2d9493d0d4e3d5d

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastproto-0.1.3-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on pkozhem/fastproto

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastproto-0.1.3-cp312-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for fastproto-0.1.3-cp312-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 a4d61a41bd5a84aff4ae9814412d899e959117b43343f97c8f2fa455fd0ed00a
MD5 16a2ffc81e96dd48d16dce329e920b86
BLAKE2b-256 594f8338ce4cf4c701dc2aebefab5d32759c71639aef5803bf44d16748b98034

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastproto-0.1.3-cp312-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: release.yml on pkozhem/fastproto

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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