Type annotations for boto3 CloudWatch
mypy-boto3-cloudwatch provides type annotations for the `boto3` CloudWatch service, generated by `mypy-boto3-builder`. It enhances development with static type checking, autocompletion, and bug detection for `boto3` CloudWatch clients. The library is actively maintained with frequent updates to align with new `boto3` versions.
Warnings
- breaking Python 3.8 support has been removed since version 8.12.0 of `mypy-boto3-builder` (which generates this package). Ensure your project uses Python 3.9 or higher.
- breaking The `mypy-boto3` ecosystem has migrated to PEP 561-compatible packages since builder version 8.12.0. This means you should now directly install `mypy-boto3-cloudwatch` rather than relying on `boto3-stubs` for service-specific types.
- breaking Breaking changes in `mypy-boto3-builder` 8.9.0 introduced shorter TypeDef names (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`) and moved `Extra` postfixes. If you directly imported and used these TypeDefs, your code may break.
- gotcha This package provides *only* type annotations. You must install `boto3` separately to have the runtime functionality for interacting with AWS services.
- gotcha While explicit type annotations are often beneficial, recent `mypy-boto3` versions offer auto-discovery for `boto3.client` and `boto3.resource` calls. Your IDE (e.g., VSCode with Pylance, PyCharm with mypy plugin) should pick up types automatically without explicit imports of `CloudWatchClient` in many cases.
- gotcha For Pylint compatibility and to avoid runtime dependencies on `mypy-boto3-cloudwatch` in production, it is recommended to guard type imports with `if TYPE_CHECKING:` to set types to `object` at runtime.
Install
-
pip install mypy-boto3-cloudwatch boto3
Imports
- CloudWatchClient
from mypy_boto3_cloudwatch.client import CloudWatchClient
- PutMetricDataInputRequestTypeDef
from mypy_boto3_cloudwatch.type_defs import PutMetricDataInputRequestTypeDef
Quickstart
import boto3
from mypy_boto3_cloudwatch.client import CloudWatchClient
from mypy_boto3_cloudwatch.type_defs import PutMetricDataInputRequestTypeDef
# Instantiate a CloudWatch client with type annotation
client: CloudWatchClient = boto3.client(
"cloudwatch",
region_name="us-east-1",
aws_access_key_id="AKIATESTKEY", # Use os.environ.get('AWS_ACCESS_KEY_ID', '') in real code
aws_secret_access_key="YOUR_SECRET_KEY" # Use os.environ.get('AWS_SECRET_ACCESS_KEY', '') in real code
)
# Example of using a TypeDef for a request payload
metric_data: PutMetricDataInputRequestTypeDef = {
"Namespace": "MyApplication",
"MetricData": [
{
"MetricName": "CustomMetric",
"Dimensions": [
{"Name": "Environment", "Value": "Production"}
],
"Value": 10.5,
"Unit": "Count",
}
],
}
try:
response = client.put_metric_data(**metric_data)
print(f"Successfully put metric data: {response['ResponseMetadata']['HTTPStatusCode']}")
except Exception as e:
print(f"Error putting metric data: {e}")