Generates mypy stub files from Protocol Buffer specifications to enable static type checking in Python.
mypy-protobuf is a tool that generates mypy stub files (.pyi) from Protocol Buffer (.proto) specifications. It enables static type checking for Python code that uses protobuf-generated classes, catching type mismatches and improving code reliability. The plugin integrates with the protoc compiler to produce accurate type annotations that reflect protobuf's field presence rules and semantics.
Python developers working with Protocol Buffers who use mypy or pyright for static type checking and want type-safe interactions with protobuf-generated code.
It provides more accurate and feature-rich type stubs compared to the built-in protobuf compiler, including GRPC service typing, proper field presence handling, and comment preservation, leading to fewer runtime errors and better developer tooling.
open source tools to generate mypy stubs from protobufs
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Generates correctly typed HasField, WhichOneof, and ClearField methods based on protobuf semantics, catching type errors that the built-in compiler misses, as detailed in the feature comparisons.
Provides type stubs for GRPC services and clients, unlike the standard protobuf compiler, enabling type-safe RPC development without manual annotations.
Preserves comments from .proto files as docstrings in generated .pyi files, enhancing IDE autocompletion and documentation for better developer experience.
Wraps enum values in NewType to prevent accidental mixing with integers, a common source of bugs, by generating enum type wrappers that enforce type distinctions.
Requires specific versions of protoc, Python-protobuf, and other tools (e.g., protoc >= 3.20, Python >= 3.9), which can conflict with existing project dependencies and complicate upgrades.
Involves multiple installation steps, command-line flags, and plugin integration with protoc, adding overhead compared to using the built-in protobuf compiler alone.
Primarily optimized for mypy and pyright; other type checkers are not continuously tested, potentially leading to unsupported configurations, as noted in the README.