How danielgtaylor/python-betterproto works
Project type and purpose
• This is an open-source Python library plus a protoc plugin (“betterproto”) that generates idiomatic Python code for Protobuf3 and gRPC.
• It consists of two main deliverables:
– A runtime library (src/betterproto/...) that provides base classes, serialization, gRPC stubs, casing, utilities, and bundled well-known types.
– A compiler plugin (src/betterproto/plugin/... and src/betterproto/compile/...) that protoc (or grpc_tools.protoc) invokes to parse .proto files and emit Python code.
File structure
• Top-level: configuration (pyproject.toml, pytest.ini), CI workflows, docs, benchmarks, tests, the library source under src/betterproto, and the plugin under src/betterproto/plugin and src/betterproto/compile.
• src/betterproto/compile: parsing, naming, importing modules—turn .proto AST into an internal representation.
• src/betterproto/plugin: plugin entrypoint (main.py, compiler.py), model validation, templating (.j2 files in templates), typing compiler, command-line interface.
• src/betterproto/grpc: grpclib-based client/server stubs generator and utilities.
• src/betterproto/lib/google and /std: pre-compiled well-known types for performance.
• src/betterproto/_types.py, casing.py, utils.py: shared helpers for code generation and runtime.
• tests and benchmarks: verify code generation output and runtime behavior.
README for additional insights
• High-level architecture: protoc → plugin → generated code → user code + runtime library.
• Supports JSON & binary serialization, async gRPC (via grpclib), Pydantic dataclasses as an optional codegen mode.
• Injection points: --python_betterproto_opt flags to modify templates (e.g. pydantic, typing imports).
• Tested via both “standard tests” (data-driven proto/json directories) and “custom tests” (pytest).