Google API Python Client Stubs
This package provides type stubs for the `google-api-python-client` library, enabling type checking with tools like `mypy` and improving autocompletion in IDEs. The stubs are automatically generated based on Google's Discovery Documents. It is not officially affiliated with Google, and its releases can be infrequent.
Warnings
- breaking Type classes and `TypedDict`s provided by `google-api-python-client-stubs` are design-time artifacts and do not exist at runtime. Direct runtime imports or unquoted explicit annotations (in Python < 3.9 without `from __future__ import annotations`) will cause `NameError` or `ModuleNotFoundError`.
- gotcha Type inference for `googleapiclient.discovery.build` can be slow in `mypy` or IDEs due to the large number of overloads generated for various services and versions. This can impact autocompletion performance.
- gotcha Stubs for non-API-specific parts of the `google-api-python-client` library (e.g., parts of `googleapiclient.http` or core utility functions) are less detailed compared to the API-specific service definitions, often defaulting to `Any`.
- gotcha The `google-api-python-client-stubs` library is independently maintained and not officially affiliated with Google. Its release cycle can be infrequent.
Install
-
pip install google-api-python-client-stubs
Imports
- build
from googleapiclient.discovery import build
- SheetsResource
from googleapiclient._apis.sheets.v4.resources import SheetsResource
Quickstart
import os
import typing
from googleapiclient.discovery import build
# Ensure annotations are processed correctly for type checkers
from __future__ import annotations
# Import the type stub for a specific service (e.g., Google Sheets v4)
# This import is solely for type checking and will not be evaluated at runtime.
if typing.TYPE_CHECKING:
from googleapiclient._apis.sheets.v4.resources import SheetsResource
# Use a placeholder for the API key for demonstration purposes
# In a real application, you would use proper authentication (e.g., OAuth2.0)
# or retrieve from environment variables.
API_KEY = os.environ.get("GOOGLE_API_KEY", "YOUR_API_KEY")
def main():
# Build the service client (runtime code for the actual API interaction)
service = build("sheets", "v4", developerKey=API_KEY)
# Example of type-hinting the service object using the imported stub type.
# This provides better IDE autocompletion and static type checking.
sheets_service: SheetsResource = build("sheets", "v4", developerKey=API_KEY)
print(f"Service built successfully. Runtime type: {type(service)}")
print("IDE and type checkers will now recognize 'sheets_service' as SheetsResource.")
# Example API call (commented out as it requires a valid API key/credentials)
# try:
# spreadsheet_id = 'YOUR_SPREADSHEET_ID'
# range_name = 'Sheet1!A1:B2'
# result = sheets_service.spreadsheets().values().get(
# spreadsheetId=spreadsheet_id,
# range=range_name
# ).execute()
# print(f"Retrieved data: {result}")
# except Exception as e:
# print(f"Error accessing Sheets API: {e}")
if __name__ == "__main__":
main()