Skip to main content

Import .proto files directly: `from magicproto.greet import hello_pb2` with no protoc build step.

Project description

magicproto

Import .proto files directly. No protoc, no generated _pb2.py files, no build step:

import magicproto                       # installs the import hook
from magicproto.greet import hello_pb2, hello_pb2_grpc

req = hello_pb2.HelloRequest(name="world")
stub = hello_pb2_grpc.GreeterStub(channel)

magicproto.greet.hello_pb2 compiles greet/hello.proto (found on MAGICPROTO_PATH / sys.path) at import time and synthesizes the module. The dotted Python path mirrors the canonical proto path 1:1, so the import name, the file location, and the descriptor name can never drift apart — the classic grpc_tools "the generated import points at the wrong place" problem cannot occur.

How it works

  1. A scoped sys.meta_path finder claims names under magicproto. (only).
  2. magicproto.greet.hello_pb2 → canonical greet/hello.proto, located on the include roots (the protoc -I model: MAGICPROTO_PATH then sys.path).
  3. The Rust extension (magicproto._compiler, built on the pure-Rust protox compiler) turns the .proto into a serialized FileDescriptorSet — the one thing the stock protobuf runtime can't do itself.
  4. Those descriptors are registered through the stock descriptor_pool.Default() + google.protobuf.internal.builder, i.e. the exact path a generated _pb2.py uses, so the message classes are indistinguishable from generated ones.
  5. _pb2_grpc modules are synthesized directly from the service descriptors.

See DESIGN.md for the full rationale, the multi-package namespacing model, and the limitations.

Where to put protos

Put foo/bar.proto where you'd want foo/bar.py, and import it as magicproto.foo.bar_pb2.

A library ships its protos as package data inside its own package directory; the directory name namespaces them, so two installed libraries can't collide.

Build (development)

python -m venv --system-site-packages .venv
source .venv/bin/activate
maturin develop
python examples/demo.py
pytest

Status

Prototype. Reuses the stock protobuf runtime for messages; a production build would route descriptor construction through a dedicated upb-backed runtime instead. See DESIGN.md § Limitations.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

magicproto-0.1.0a0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (958.4 kB view details)

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

magicproto-0.1.0a0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (825.4 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

File details

Details for the file magicproto-0.1.0a0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for magicproto-0.1.0a0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cf08b73eea984414ecb61dc1c87379309f1a3674520a482bdd53bd418f3764f6
MD5 1c1470f794d10d09b343b6e696571323
BLAKE2b-256 9f8609d76fe64da85b5943eb85ea0f132286b3c9dc3cc5d30f9eb4d9321ce399

See more details on using hashes here.

File details

Details for the file magicproto-0.1.0a0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for magicproto-0.1.0a0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e75dfcbb8fcd5e5238376c63f2eb5987ce8b650684480d1fd2ded5f30d0909b7
MD5 9d76f77ef485c579acdbcd79346e5b5f
BLAKE2b-256 3e160ea38ffdec3b74e23400b62dd26aeb457d896417f8a122fdcca586f66c3c

See more details on using hashes here.

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