Type annotations for boto3 ServiceQuotas

1.42.10 · active · verified Sat Apr 11

mypy-boto3-service-quotas provides drop-in type annotations for the boto3 ServiceQuotas client. It enhances static type checking with tools like mypy and pyright, and improves autocompletion in IDEs such as VSCode and PyCharm, by leveraging type definitions extracted from botocore schemas. This package is generated automatically with mypy-boto3-builder and is currently at version 1.42.10, with frequent updates to align with new boto3 releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a typed ServiceQuotas client and make a basic API call to list service quotas, leveraging type hints for improved code quality and IDE support. It includes a fallback for dummy AWS credentials if not already configured, making it directly runnable for testing type checking.

import os
from typing import TYPE_CHECKING

import boto3

# For runtime, boto3 client usually works without explicit import from stubs
# For type checking, import the specific client and type defs
if TYPE_CHECKING:
    from mypy_boto3_service_quotas.client import ServiceQuotasClient
    from mypy_boto3_service_quotas.type_defs import ListServiceQuotasOutputTypeDef

def get_service_quotas_client() -> "ServiceQuotasClient":
    """Returns a typed Service Quotas client."""
    # This assumes AWS credentials are configured via environment variables,
    # shared credentials file, or IAM role.
    return boto3.client(
        "service-quotas", region_name=os.environ.get("AWS_REGION", "us-east-1")
    )

def list_quotas():
    """Lists service quotas and prints the first few."""
    client = get_service_quotas_client()
    try:
        response: "ListServiceQuotasOutputTypeDef" = client.list_service_quotas(
            ServiceCode="ec2" # Example service code, change as needed
        )
        quotas = response.get("Quotas", [])
        if quotas:
            print(f"Found {len(quotas)} EC2 quotas. First 3:")
            for quota in quotas[:3]:
                print(f"- {quota.get('QuotaName')}: {quota.get('Value')} {quota.get('Unit')}")
        else:
            print("No EC2 quotas found for 'ec2' service code.")
    except Exception as e:
        print(f"Error listing quotas: {e}")

if __name__ == "__main__":
    # Set dummy AWS credentials for local testing without actual auth
    # In a real scenario, these would be properly configured.
    if "AWS_ACCESS_KEY_ID" not in os.environ:
        os.environ["AWS_ACCESS_KEY_ID"] = os.environ.get("AWS_ACCESS_KEY_ID", "TEST_KEY")
        os.environ["AWS_SECRET_ACCESS_KEY"] = os.environ.get("AWS_SECRET_ACCESS_KEY", "TEST_SECRET")
        os.environ["AWS_SESSION_TOKEN"] = os.environ.get("AWS_SESSION_TOKEN", "TEST_TOKEN")
        os.environ["AWS_REGION"] = os.environ.get("AWS_REGION", "us-east-1") # Required for many AWS calls

    print("Attempting to list service quotas...")
    list_quotas()

view raw JSON →