Type Stubs for Google Auth
google-auth-stubs provides type stubs for the `google-auth` library, enabling static type checking (e.g., with MyPy) for Google authentication code. It helps catch type-related errors at development time without affecting runtime behavior. The current version is 0.3.0, with releases occurring as needed to support new `google-auth` versions or add type elaborations.
Warnings
- gotcha This library provides type stubs only. You must also install the actual `google-auth` library for your application to run at runtime. `google-auth-stubs` is a development-time dependency for type checking.
- gotcha Type stubs may not perfectly align with every minor version of the `google-auth` library. Mismatched versions can lead to incorrect type checking results or false positives from your type checker.
- gotcha The library explicitly requires Python versions >=3.6.2 and <4.0.0. Using it with unsupported Python versions may lead to installation failures or unexpected behavior.
Install
-
pip install google-auth-stubs
Imports
- Credentials
from google.auth.credentials import Credentials
- default
import google.auth.default
- Request
from google.auth.transport.requests import Request
- service_account_credentials
from google.oauth2 import service_account
Quickstart
import google.auth
from google.auth.transport.requests import Request
# This code demonstrates typical google-auth usage, which google-auth-stubs provides types for.
# In a real application, credentials would be obtained from environment variables,
# a service account file, or other configuration.
# For example, to use default credentials:
try:
credentials, project = google.auth.default()
# If credentials are refreshable and expired, try to refresh them.
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
print(f"Successfully obtained credentials for project: {project}")
print(f"Credentials scope(s): {credentials.scopes}")
except Exception as e:
print(f"Failed to obtain Google Cloud credentials: {e}")
print("Ensure 'google-auth' is installed and credentials are configured (e.g., GOOGLE_APPLICATION_CREDENTIALS).")