Skip to main content

grapec

Declare plain Python classes, call gRPC and thrift services with them.

import grapec


@grapec.struct(package="connectrpc.eliza.v1")
class SayRequest:
    sentence: str


@grapec.struct(package="connectrpc.eliza.v1")
class SayResponse:
    sentence: str


class ElizaService(grapec.Client, package="connectrpc.eliza.v1"):
    @grapec.name("Say")
    def say(self, request: SayRequest) -> SayResponse: ...


eliza = ElizaService("grpcs://demo.connectrpc.com")
reply = eliza.say(SayRequest(sentence="I feel tired today"))
print(reply.sentence)

That talks to the public ELIZA demo at demo.connectrpc.com, paste it into a file and run it.

The type hints are the schema. @grapec.struct turns a class into a keyword only dataclass that encodes and decodes itself with the protobuf wire format or the thrift binary protocol, and grapec.Client subclasses call methods on a standard gRPC or thrift server. There are no .proto or .thrift files to write and no generated code to check in. The only dependency is h2.

Pronounced /ɡreɪ.peɪk/, like "gray-pay-k".

Install

Requires Python 3.12+.

pip install grapec
# or
uv add grapec

Example

import enum
from datetime import datetime

import grapec


class Priority(enum.IntEnum):
    UNSPECIFIED = 0
    LOW = 1
    HIGH = 2


@grapec.struct(package="example.hello.v1")
class Tag:
    key: str
    value: str


@grapec.struct(package="example.hello.v1")
class HelloRequest:
    name: str
    priority: Priority = Priority.LOW
    tags: list[Tag]                  # repeated Tag
    sent_at: datetime | None         # optional google.protobuf.Timestamp


@grapec.struct(package="example.hello.v1")
class HelloReply:
    message: str


class Greeter(grapec.Client, package="example.hello.v1"):
    @grapec.name("SayHello")
    def say_hello(self, request: HelloRequest) -> HelloReply: ...


greeter = Greeter("grpc://localhost:50051", timeout=5)
reply = greeter.say_hello(HelloRequest(name="grapec", tags=[Tag(key="lang", value="python")]))

data = bytes(reply)                          # protobuf wire format
HelloReply.from_bytes(data)
reply.to_json()                              # proto3 JSON mapping
print(grapec.export_proto(Greeter))          # the .proto for the other side

Field numbers follow declaration order, Annotated[int, grapec.Id(7)] pins one. The URL scheme picks the transport, grpc://, grpcs://, thrift:// or thrifts://. Connections are pooled per client, calls accept timeout=, metadata= and compression=. For asyncio, subclass grapec.AsyncClient and declare the methods with async def.

The same structs work with thrift. Methods may take several parameters, return scalars and declare the exceptions they throw:

from typing import Annotated

import grapec


@grapec.struct(package="store")
class NotFound(Exception):
    key: str


@grapec.struct(package="store")
class Item:
    key: str
    count: Annotated[int, grapec.I32]


class Store(grapec.Client, package="store"):
    @grapec.raises(NotFound)
    def get(self, key: str) -> Item: ...

    def put(self, item: Item) -> None: ...


store = Store("thrift://localhost:9090")
try:
    item = store.get("answer")
except NotFound as exc:
    print("no such key", exc.key)

More

Current scope is struct serialization and unary calls. Streaming is not implemented yet.

Development

uv sync
uv run pytest

Serialization is tested against the protobuf and thriftpy2 implementations, the clients against real grpcio and thrift servers.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

grapec-0.3.0.tar.gz (93.9 kB view details)

Uploaded Source

Built Distribution

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

grapec-0.3.0-py3-none-any.whl (46.3 kB view details)

Uploaded Python 3

File details

Details for the file grapec-0.3.0.tar.gz.

File metadata

  • Download URL: grapec-0.3.0.tar.gz
  • Upload date:
  • Size: 93.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for grapec-0.3.0.tar.gz
Algorithm Hash digest
SHA256 67cc5f051f9bce008d1624b9242fa8c9d050daf2a3d3be1e1db745ec1aadb0fd
MD5 f873ff69cff33bed6e1e7f2629e97242
BLAKE2b-256 3cb87ff5db44f59deb6930a26d65f1b3b849eba792042e2fe6d9977a88fbeb39

See more details on using hashes here.

Provenance

The following attestation bundles were made for grapec-0.3.0.tar.gz:

Publisher: release.yml on aisk/grapec

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

File details

Details for the file grapec-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: grapec-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 46.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for grapec-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd79e60d91c946d4d382c5b71939cfb500bc6dd954656eab2711cda6c04c3ce1
MD5 6affec5d856f9131e54c2efdf21ee7b3
BLAKE2b-256 2e0ebc575e851813ebf000111d93552dab7edbf573ad9cf80977c69fe5146d01

See more details on using hashes here.

Provenance

The following attestation bundles were made for grapec-0.3.0-py3-none-any.whl:

Publisher: release.yml on aisk/grapec

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

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page