Skip to content

Examples#

Index > CloudWatch > Examples

Auto-generated documentation for CloudWatch type annotations stubs module mypy-boto3-cloudwatch.

Client#

Implicit type annotations#

Can be used with boto3-stubs[cloudwatch] package installed.

Write your CloudWatch code as usual, type checking and code completion should work out of the box.

# CloudWatchClient usage example

from boto3.session import Session


session = Session()

client = session.client("cloudwatch")  # (1)
result = client.delete_alarms()  # (2)
  1. client: CloudWatchClient
  2. result: EmptyResponseMetadataTypeDef
# DescribeAlarmHistoryPaginator usage example

from boto3.session import Session


session = Session()
client = session.client("cloudwatch")  # (1)

paginator = client.get_paginator("describe_alarm_history")  # (2)
for item in paginator.paginate(...):
    print(item)  # (3)
  1. client: CloudWatchClient
  2. paginator: DescribeAlarmHistoryPaginator
  3. item: DescribeAlarmHistoryOutputTypeDef
# AlarmExistsWaiter usage example

from boto3.session import Session


session = Session()
client = session.client("cloudwatch")  # (1)

waiter = client.get_waiter("alarm_exists")  # (2)
waiter.wait()
  1. client: CloudWatchClient
  2. waiter: AlarmExistsWaiter

Explicit type annotations#

With boto3-stubs-lite[cloudwatch] or a standalone mypy_boto3_cloudwatch package, you have to explicitly specify client: CloudWatchClient type annotation.

All other type annotations are optional, as types should be discovered automatically. However, these type annotations can be helpful in your functions and methods.

# CloudWatchClient usage example with type annotations

from boto3.session import Session

from mypy_boto3_cloudwatch.client import CloudWatchClient
from mypy_boto3_cloudwatch.type_defs import EmptyResponseMetadataTypeDef
from mypy_boto3_cloudwatch.type_defs import DeleteAlarmsInputRequestTypeDef


session = Session()

client: CloudWatchClient = session.client("cloudwatch")

kwargs: DeleteAlarmsInputRequestTypeDef = {...}
result: EmptyResponseMetadataTypeDef = client.delete_alarms(**kwargs)
# DescribeAlarmHistoryPaginator usage example with type annotations

from boto3.session import Session

from mypy_boto3_cloudwatch.client import CloudWatchClient
from mypy_boto3_cloudwatch.paginator import DescribeAlarmHistoryPaginator
from mypy_boto3_cloudwatch.type_defs import DescribeAlarmHistoryOutputTypeDef


session = Session()
client: CloudWatchClient = session.client("cloudwatch")

paginator: DescribeAlarmHistoryPaginator = client.get_paginator("describe_alarm_history")
for item in paginator.paginate(...):
    item: DescribeAlarmHistoryOutputTypeDef
    print(item)
# AlarmExistsWaiter usage example with type annotations

from boto3.session import Session

from mypy_boto3_cloudwatch.client import CloudWatchClient
from mypy_boto3_cloudwatch.waiter import AlarmExistsWaiter

session = Session()
client: CloudWatchClient = session.client("cloudwatch")

waiter: AlarmExistsWaiter = client.get_waiter("alarm_exists")
waiter.wait()

Service Resource#

Implicit type annotations#

Can be used with boto3-stubs[cloudwatch] package installed.

# CloudWatchServiceResource usage example

from boto3.session import Session


session = Session()

resource = session.resource("cloudwatch")  # (1)
result = resource.Alarm()  # (2)
  1. resource: CloudWatchServiceResource
  2. result:
# ServiceResourceAlarmsCollection usage example

from boto3.session import Session


session = Session()
resource = session.resource("cloudwatch")  # (1)

collection = resource.alarms  # (2)
for item in collection:
    print(item)  # (3)
  1. resource: CloudWatchServiceResource
  2. collection: CloudWatchServiceResource
  3. item: Alarm

Explicit type annotations#

With boto3-stubs-lite[cloudwatch] or a standalone mypy_boto3_cloudwatch package, you have to explicitly specify resource: CloudWatchServiceResource type annotation.

All other type annotations are optional, as types should be discovered automatically. However, these type annotations can be helpful in your functions and methods.

# CloudWatchServiceResource usage example with type annotations

from boto3.session import Session

from mypy_boto3_cloudwatch.service_resource import CloudWatchServiceResource
from mypy_boto3_cloudwatch.service_resource import Alarm


session = Session()

resource: CloudWatchServiceResource = session.resource("cloudwatch")
result: Alarm = resource.Alarm()
# ServiceResourceAlarmsCollection usage example with type annotations

from boto3.session import Session

from mypy_boto3_cloudwatch.service_resource import CloudWatchServiceResource
from mypy_boto3_cloudwatch.service_resource import ServiceResourceAlarmsCollection
from mypy_boto3_cloudwatch.service_resource import Alarm


session = Session()

resource: CloudWatchServiceResource = session.resource("cloudwatch")

collection: ServiceResourceAlarmsCollection = resource.alarms
for item in collection:
    item: Alarm
    print(item)