Type Annotations for aiobotocore CloudWatch
This library provides comprehensive type annotations (stubs) for the `aiobotocore` CloudWatch service client. It enables static type checkers like MyPy to validate the usage of `aiobotocore.client.CloudWatch` methods, parameters, and return types, significantly enhancing code quality and reducing runtime errors. Generated by `mypy-boto3-builder`, it is updated frequently to align with the latest AWS API definitions and `aiobotocore` releases.
Warnings
- breaking Starting with `mypy-boto3-builder` version 8.12.0 (which generated `types-aiobotocore-cloudwatch` 3.4.0), Python 3.8 is no longer supported for these type stubs. The package requires Python >= 3.9.
- gotcha As of `mypy-boto3-builder` version 8.12.0, all `types-aiobotocore` packages officially migrated to PEP 561 native namespace packages. While type checkers generally find stubs, for explicit and robust type hinting, it is recommended to directly import types (like `CloudWatchClient` or `TypeDefs`) from the `types_aiobotocore_cloudwatch` package rather than relying on inferred types from `aiobotocore`'s runtime imports.
- breaking `mypy-boto3-builder` version 8.9.0 introduced changes to `TypeDef` naming conventions. Some argument TypeDefs might have shorter names (e.g., `RequestRequestTypeDef` became `RequestTypeDef`), and `Extra` postfixes might have moved. While not every service is affected in the same way, verify your custom `TypeDef` usage if you encounter type-checking errors related to dictionary shapes.
Install
-
pip install types-aiobotocore-cloudwatch aiobotocore
Imports
- CloudWatchClient
from types_aiobotocore_cloudwatch.client import CloudWatchClient
- PutMetricDataInputRequestTypeDef
from types_aiobotocore_cloudwatch.type_defs import PutMetricDataInputRequestTypeDef
Quickstart
import asyncio
import os
from aiobotocore.session import get_session
from types_aiobotocore_cloudwatch.client import CloudWatchClient
from types_aiobotocore_cloudwatch.type_defs import PutMetricDataInputRequestTypeDef
async def put_cloudwatch_metric():
session = get_session()
# Ensure AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION are set as environment variables
async with session.create_client(
"cloudwatch",
aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID", ""),
aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY", ""),
region_name=os.environ.get("AWS_REGION", "us-east-1")
) as client:
# client is automatically type-hinted as CloudWatchClient by static analysis
# For explicit hinting:
cloudwatch_client: CloudWatchClient = client
metric_data: PutMetricDataInputRequestTypeDef = {
"Namespace": "MyApplication",
"MetricData": [
{
"MetricName": "RequestCount",
"Dimensions": [
{"Name": "Environment", "Value": "Production"},
],
"Value": 1.0,
"Unit": "Count",
},
],
}
response = await cloudwatch_client.put_metric_data(
Namespace=metric_data["Namespace"],
MetricData=metric_data["MetricData"]
)
print(f"PutMetricData response: {response}")
if __name__ == "__main__":
# Example usage: set dummy env vars if not truly interacting with AWS
os.environ.setdefault("AWS_ACCESS_KEY_ID", "DUMMY_KEY")
os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "DUMMY_SECRET")
os.environ.setdefault("AWS_REGION", "us-east-1")
asyncio.run(put_cloudwatch_metric())