mypy-boto3-compute-optimizer-automation Type Stubs

1.42.3 · active · verified Sat Apr 11

This library provides type annotations (stubs) for the `boto3` AWS SDK, specifically for the `ComputeOptimizerAutomation` service. It enables static type checking with tools like Mypy and enhances IDE auto-completion for `boto3` operations, improving code quality and reducing runtime errors. The current version is 1.42.3, and it is part of the frequently updated `mypy-boto3-builder` project.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a type-hinted `ComputeOptimizerAutomationClient` and use it to list automation rules, leveraging type annotations for improved developer experience and static analysis. It uses environment variables for AWS credentials, suitable for local execution and CI/CD pipelines.

import os
from typing import TYPE_CHECKING
import boto3

# These imports are only for type checking, not for runtime execution
if TYPE_CHECKING:
    from mypy_boto3_compute_optimizer_automation import ComputeOptimizerAutomationClient
    from mypy_boto3_compute_optimizer_automation.type_defs import ListAutomationRulesResponseTypeDef

def get_compute_optimizer_automation_client() -> "ComputeOptimizerAutomationClient":
    """
    Provides a type-hinted ComputeOptimizerAutomation client.
    Ensure AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are set in environment.
    """
    session = boto3.Session(
        aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID", "dummy"),
        aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY", "dummy"),
        region_name=os.environ.get("AWS_REGION", "us-east-1")
    )
    return session.client("compute-optimizer-automation")

def list_all_automation_rules():
    """Lists all Compute Optimizer Automation rules with type hints."""
    # Explicitly type the client for better IDE support and type checking
    client: ComputeOptimizerAutomationClient = get_compute_optimizer_automation_client()
    
    print("Attempting to list Compute Optimizer Automation Rules...")
    try:
        # Type the response for detailed type checking of the result
        response: ListAutomationRulesResponseTypeDef = client.list_automation_rules()
        
        rules = response.get("AutomationRules", [])
        if rules:
            print("Compute Optimizer Automation Rules found:")
            for rule in rules:
                print(f"  - Name: {rule.get('RuleName', 'N/A')}, ARN: {rule.get('RuleArn', 'N/A')}")
        else:
            print("No Compute Optimizer Automation rules found.")
        
        if response.get("NextToken"):
            print(" (More rules available, consider using a paginator for a full list)")
    except Exception as e:
        print(f"Error listing rules: {e}")
        print("Please ensure your AWS credentials and region are correctly configured.")

if __name__ == "__main__":
    # Set dummy environment variables if not already set for runnable example
    # In a real scenario, these should be valid AWS credentials
    os.environ.setdefault('AWS_ACCESS_KEY_ID', 'AKIAIOSFODNN7EXAMPLE')
    os.environ.setdefault('AWS_SECRET_ACCESS_KEY', 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY')
    os.environ.setdefault('AWS_REGION', 'us-east-1')
    list_all_automation_rules()

view raw JSON →