mypy-boto3-compute-optimizer-automation Type Stubs
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
- breaking Python 3.8 support was removed from `mypy-boto3-builder` version `8.12.0` (which generates these stubs). Projects using this library now require Python 3.9 or newer.
- breaking In `mypy-boto3-builder` version `8.9.0`, TypeDef naming conventions were changed (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`). This might break existing explicit type annotations in your code.
- gotcha This package provides *type stubs* for static analysis with tools like Mypy or IDEs. It does *not* include the `boto3` library itself. `boto3` must be installed separately for your application to run at runtime.
- gotcha For optimal IDE support (VSCode, PyCharm) and robust static type checking, it is often recommended to use *explicit type annotations* for `boto3.client` and `boto3.session.client` calls, rather than relying solely on implicit type discovery.
- gotcha When using `mypy-boto3` packages with `pylint`, you might encounter 'undefined variable' warnings if types are guarded by `if TYPE_CHECKING:`. A common workaround is to conditionally set types to `object` outside of the `TYPE_CHECKING` block to satisfy `pylint`.
Install
-
pip install mypy-boto3-compute-optimizer-automation -
pip install boto3
Imports
- ComputeOptimizerAutomationClient
from mypy_boto3_compute_optimizer_automation import ComputeOptimizerAutomationClient
- ListAutomationRulesResponseTypeDef
from mypy_boto3_compute_optimizer_automation.type_defs import ListAutomationRulesResponseTypeDef
- ComputeOptimizerAutomationPaginator
from mypy_boto3_compute_optimizer_automation.paginator import ListAutomationRulesPaginator
Quickstart
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()