Typing stubs for Protobuf
raw JSON → 6.32.1.20260221 verified Tue May 12 auth: no python install: stale quickstart: stale
types-protobuf provides type stub files for the `protobuf` (also known as `google-protobuf`) library. It allows type checkers like Mypy or Pyright to perform static analysis on Python code that uses Protocol Buffers, improving code quality and catching potential type errors. This package is part of the `typeshed` project and aims to provide accurate annotations for `protobuf~=6.32.1`.
pip install types-protobuf Common errors
error Cannot find implementation or library stub for module 'google.protobuf' ↓
cause Mypy cannot find the type stub files for the `google.protobuf` library, usually because `types-protobuf` is not installed or Mypy cannot locate the installed stubs.
fix
pip install types-protobuf
error ModuleNotFoundError: No module named 'google.protobuf' ↓
cause This error occurs when a type checker or runtime environment tries to import `google.protobuf`, but the actual `protobuf` runtime library (which `types-protobuf` provides stubs for) is not installed.
fix
pip install protobuf
error error: "Message" is not a known member of module "google.protobuf.message" ↓
cause The type checker is unable to find specific type information (like the `Message` class) within `google.protobuf.message`, often due to `types-protobuf` being missing, outdated, or a version mismatch with the installed `protobuf` library.
fix
Ensure both
protobuf and types-protobuf are installed and their versions are compatible. Consider upgrading both: pip install --upgrade protobuf types-protobuf. Warnings
breaking Changes in `.proto` schema definitions (e.g., field renumbering, removal, or type alteration) can lead to runtime deserialization failures and will cause type checking errors if not properly managed. This is a fundamental aspect of Protobuf evolution, and `types-protobuf` will reflect these underlying API changes. ↓
fix Use a robust schema evolution strategy (e.g., deprecate fields instead of deleting, avoid renumbering, maintain compatibility across versions). Tools like `buf` can help lint `.proto` files for breaking changes.
gotcha `types-protobuf` is a partial stub package, meaning not all annotations for the `protobuf` library might be present. You might encounter `Missing type annotation` errors for some less common parts of the API. ↓
fix If you encounter missing annotations, consider contributing to the `typeshed` project where `types-protobuf` stubs are maintained. For immediate local fixes, you can use `type: ignore` or local stub files.
gotcha A mismatch between the installed `types-protobuf` version and the `protobuf` runtime library version can lead to incorrect type checking results. The stubs are generated against specific runtime versions (e.g., `protobuf~=6.32.1`). ↓
fix Ensure that `types-protobuf` and `protobuf` versions are compatible. Typeshed recommends pinning stub packages to a known good version (e.g., `types-protobuf==X.Y.Z.build`) or aligning version bounds (e.g., `protobuf>=A.B.C,<A.B.D` and `types-protobuf>=A.B.C,<A.B.D`).
gotcha The handling of `Optional[type]` for protobuf wrapper classes (like `StringValue` or `Int32Value`) can be a source of type errors. In older `proto3` definitions or with specific `mypy-protobuf` configurations, values like `StringValue(value=None)` might pass at runtime but fail type checking if the stub expects `str` instead of `Optional[str]`. ↓
fix Ensure consistency in how `None` values are handled. If using `mypy-protobuf` for generating custom stubs, check `relax_strict_optional_primitives` options. For well-known wrapper types, refer to `typeshed`'s latest stubs and `protobuf` documentation on explicit presence.
breaking The `ModuleNotFoundError: No module named 'google'` indicates that the 'protobuf' runtime library itself is not installed or not accessible in the Python environment. `types-protobuf` provides type stubs for the 'protobuf' library, but it does not install the runtime library itself, which is a mandatory dependency. ↓
fix Ensure the `protobuf` package is installed in your environment (e.g., `pip install protobuf`). Verify that the Python environment where `types-protobuf` is used also has `protobuf` installed and accessible.
breaking The `protobuf` runtime library, which provides the `google.protobuf` package, must be installed for any Protobuf-related code to run. `types-protobuf` only provides type stubs and does not install the runtime library. ↓
fix Ensure the `protobuf` library is installed in your environment (e.g., `pip install protobuf`). Verify that the Python environment running the script has `protobuf` accessible.
Install compatibility stale last tested: 2026-05-12
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - - 18.2M
3.10 alpine (musl) - - - -
3.10 slim (glibc) wheel 1.5s - 19M
3.10 slim (glibc) - - - -
3.11 alpine (musl) wheel - - 20.1M
3.11 alpine (musl) - - - -
3.11 slim (glibc) wheel 1.6s - 21M
3.11 slim (glibc) - - - -
3.12 alpine (musl) wheel - - 11.9M
3.12 alpine (musl) - - - -
3.12 slim (glibc) wheel 1.5s - 12M
3.12 slim (glibc) - - - -
3.13 alpine (musl) wheel - - 11.7M
3.13 alpine (musl) - - - -
3.13 slim (glibc) wheel 1.5s - 12M
3.13 slim (glibc) - - - -
3.9 alpine (musl) wheel - - 17.7M
3.9 alpine (musl) - - - -
3.9 slim (glibc) wheel 1.8s - 18M
3.9 slim (glibc) - - - -
Imports
- Message
from google.protobuf import message - Timestamp
from google.protobuf.timestamp_pb2 import Timestamp
Quickstart stale last tested: 2026-04-24
import os
from google.protobuf.timestamp_pb2 import Timestamp # A well-known type
# --- Simulate generated _pb2.py content for a custom message ---
# In a real scenario, this would come from `my_message_pb2.py`
# generated by `protoc --python_out=. my_message.proto`
# my_message.proto content:
# syntax = "proto3";
# package mypackage;
# message MyMessage {
# string name = 1;
# int32 id = 2;
# repeated string tags = 3;
# }
# Simplified representation for quickstart (type checkers would use real stubs)
class MyMessage:
name: str
id: int
tags: list[str]
def __init__(self, name: str = '', id: int = 0, tags: list[str] | None = None) -> None:
self.name = name
self.id = id
self.tags = tags if tags is not None else []
def SerializeToString(self) -> bytes:
return b""
@classmethod
def ParseFromString(cls, serialized_data: bytes) -> 'MyMessage':
return cls()
# --- End simulated content ---
def process_message(msg: MyMessage) -> str:
"""Processes a custom protobuf message with type hints."""
print(f"Processing Message: Name={msg.name}, ID={msg.id}")
return f"Message with {len(msg.tags)} tags processed."
def get_current_timestamp() -> Timestamp:
"""Gets the current UTC timestamp using google.protobuf.Timestamp."""
ts = Timestamp()
ts.GetCurrentTime()
return ts
# Example Usage:
my_instance = MyMessage(name="ExampleUser", id=42, tags=["dev", "python"])
result = process_message(my_instance)
print(result)
timestamp_obj = get_current_timestamp()
print(f"Current UTC Timestamp: {timestamp_obj}")