Google API Python Client Stubs

1.34.0 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This quickstart demonstrates how to set up and use the `google-api-python-client-stubs` to provide type hints for a Google Sheets service client. It shows both the runtime `build` call and how to apply an explicit type annotation for improved development experience. Note the use of `if typing.TYPE_CHECKING:` to prevent runtime errors from stub-only imports.

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()

view raw JSON →