Google API Common Protos Type Stubs
The `googleapis-common-protos-stubs` library provides PEP 561-compliant type stubs for the `googleapis-common-protos` Python package. It enables static type checking tools like Mypy to understand the types provided by Google's common protocol buffer definitions, which are widely used across Google API services. The current version is 2.3.1, and it maintains an active release cadence, often aligning with updates to the underlying `googleapis-common-protos` or `mypy-protobuf` toolchain.
Common errors
-
Mypy error: Cannot find module 'google.protobuf.timestamp_pb2'
cause Mypy cannot find the type definitions for the `google.protobuf` module. This often means the `googleapis-common-protos-stubs` package is not installed or not discoverable by Mypy.fixInstall `googleapis-common-protos-stubs` via `pip install googleapis-common-protos-stubs`. Also ensure `mypy` is run with `--namespace-packages` if using v2.0.0 or later of the stubs. -
Mypy error: Namespace package 'google' not found without --namespace-packages
cause After version 2.0.0, `googleapis-common-protos-stubs` uses namespace packages. Mypy requires explicit configuration to correctly resolve imports within namespace packages.fixAdd `--namespace-packages` to your Mypy command line (e.g., `mypy --namespace-packages your_module.py`) or configure it in your `mypy.ini` or `pyproject.toml` file under `[tool.mypy]` with `namespace_packages = true`. -
Mypy error: Name 'Timestamp' is not defined (or similar name resolution error for common proto types)
cause Even with stubs installed, the runtime `googleapis-common-protos` library might be missing, or the import path for the specific protobuf type is incorrect.fixEnsure `googleapis-common-protos` is installed (`pip install googleapis-common-protos`). Double-check the import statement for the protobuf type (e.g., `from google.protobuf.timestamp_pb2 import Timestamp`).
Warnings
- breaking Version 2.0.0 of `googleapis-common-protos-stubs` introduced the use of namespace packages. This change requires Mypy users to explicitly enable namespace package support for correct type checking.
- gotcha This package provides *type stubs* (`.pyi` files) for `googleapis-common-protos`, not the runtime Python classes themselves. You should `import` the protobuf types from the `google.protobuf` or `google.api` namespace provided by the `googleapis-common-protos` package, not directly from `googleapis_common_protos_stubs`.
- gotcha The package removed `__init__.pyi` and `py.typed` files in version 2.3.1. While this is likely a correction for PEP 420 namespace package compliance, older versions of Mypy or specific tooling might expect these files for type stub discovery.
Install
-
pip install googleapis-common-protos-stubs
Imports
- Timestamp
from google.protobuf.timestamp_pb2 import Timestamp
- FieldMask
from google.protobuf.field_mask_pb2 import FieldMask
Quickstart
import os
import subprocess
# A simple Python file using a common proto type
python_code = '''
from google.protobuf.timestamp_pb2 import Timestamp
from datetime import datetime
def create_timestamp_message(dt: datetime) -> Timestamp:
timestamp = Timestamp()
timestamp.FromDatetime(dt)
return timestamp
def get_seconds(ts: Timestamp) -> int:
return ts.seconds # type: ignore [attr-defined]
# Example usage (not type-checked for this demo, just for context)
# now = datetime.now()
# ts_msg = create_timestamp_message(now)
# print(f"Created Timestamp: {ts_msg.seconds}.{ts_msg.nanos}")
'''
with open('my_proto_app.py', 'w') as f:
f.write(python_code)
# Run mypy with --namespace-packages to type-check
try:
# Using `--namespace-packages` is crucial since v2.0.0 of the stubs
result = subprocess.run(['mypy', '--namespace-packages', 'my_proto_app.py'], capture_output=True, text=True, check=True)
print("Mypy successful:")
print(result.stdout)
print("Note: 'type: ignore' used for demonstration; in real code, you'd fix type errors.")
except subprocess.CalledProcessError as e:
print(f"Mypy failed with errors:\n{e.stderr}")
finally:
os.remove('my_proto_app.py')