{"id":720,"library":"types-protobuf","title":"Typing stubs for Protobuf","description":"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`.","status":"active","version":"6.32.1.20260221","language":"python","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","protobuf","typeshed","type-checking"],"install":[{"cmd":"pip install types-protobuf","lang":"bash","label":"Install types-protobuf"}],"dependencies":[{"reason":"Provides runtime functionality for which these stubs offer type hints. The stubs are designed for `protobuf~=6.32.1`.","package":"protobuf","optional":false}],"imports":[{"note":"Imports are typically from the `google.protobuf` runtime library, not directly from `types-protobuf` itself. `types-protobuf` provides the type hints for these imports.","symbol":"Message","correct":"from google.protobuf import message"},{"note":"For well-known types like Timestamp, you import from their specific `_pb2` modules, which are generated by `protoc`.","symbol":"Timestamp","correct":"from google.protobuf.timestamp_pb2 import Timestamp"}],"quickstart":{"code":"import os\nfrom google.protobuf.timestamp_pb2 import Timestamp # A well-known type\n\n# --- Simulate generated _pb2.py content for a custom message ---\n# In a real scenario, this would come from `my_message_pb2.py`\n# generated by `protoc --python_out=. my_message.proto`\n\n# my_message.proto content:\n# syntax = \"proto3\";\n# package mypackage;\n# message MyMessage {\n#   string name = 1;\n#   int32 id = 2;\n#   repeated string tags = 3;\n# }\n\n# Simplified representation for quickstart (type checkers would use real stubs)\nclass MyMessage:\n    name: str\n    id: int\n    tags: list[str]\n\n    def __init__(self, name: str = '', id: int = 0, tags: list[str] | None = None) -> None:\n        self.name = name\n        self.id = id\n        self.tags = tags if tags is not None else []\n\n    def SerializeToString(self) -> bytes:\n        return b\"\"\n\n    @classmethod\n    def ParseFromString(cls, serialized_data: bytes) -> 'MyMessage':\n        return cls()\n# --- End simulated content ---\n\n\ndef process_message(msg: MyMessage) -> str:\n    \"\"\"Processes a custom protobuf message with type hints.\"\"\"\n    print(f\"Processing Message: Name={msg.name}, ID={msg.id}\")\n    return f\"Message with {len(msg.tags)} tags processed.\"\n\ndef get_current_timestamp() -> Timestamp:\n    \"\"\"Gets the current UTC timestamp using google.protobuf.Timestamp.\"\"\"\n    ts = Timestamp()\n    ts.GetCurrentTime()\n    return ts\n\n# Example Usage:\nmy_instance = MyMessage(name=\"ExampleUser\", id=42, tags=[\"dev\", \"python\"])\nresult = process_message(my_instance)\nprint(result)\n\ntimestamp_obj = get_current_timestamp()\nprint(f\"Current UTC Timestamp: {timestamp_obj}\")","lang":"python","description":"This quickstart demonstrates how `types-protobuf` provides static type checking for code interacting with `google.protobuf` messages. It includes a simulated custom message type (which would typically be generated from a `.proto` file) and usage of a well-known type. With `types-protobuf` installed, type checkers can verify argument types, attribute access, and return types."},"warnings":[{"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.","message":"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.","severity":"breaking","affected_versions":"All versions of protobuf and types-protobuf"},{"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.","message":"`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.","severity":"gotcha","affected_versions":"All versions"},{"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`).","message":"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`).","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"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]`.","severity":"gotcha","affected_versions":"Versions prior to `protobuf` edition 2023's explicit presence default."},{"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.","message":"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.","severity":"breaking","affected_versions":"All versions"},{"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.","message":"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.","severity":"breaking","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T18:15:52.190Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"pip install types-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.","error":"Cannot find implementation or library stub for module 'google.protobuf'"},{"fix":"pip install 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.","error":"ModuleNotFoundError: No module named 'google.protobuf'"},{"fix":"Ensure both `protobuf` and `types-protobuf` are installed and their versions are compatible. Consider upgrading both: `pip install --upgrade protobuf types-protobuf`.","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.","error":"error: \"Message\" is not a known member of module \"google.protobuf.message\""}],"ecosystem":"pypi","meta_description":null,"install_score":0,"install_tag":"stale","quickstart_score":0,"quickstart_tag":"stale","pypi_latest":"7.34.1.20260508","install_checks":{"last_tested":"2026-05-12","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"18.2M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":null,"mem_mb":null,"disk_size":"19M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"20.1M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.6,"import_time_s":null,"mem_mb":null,"disk_size":"21M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"11.9M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":null,"mem_mb":null,"disk_size":"12M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"11.7M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":null,"mem_mb":null,"disk_size":"12M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"17.7M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.8,"import_time_s":null,"mem_mb":null,"disk_size":"18M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":1,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}}