mypy-boto3-cloudformation
mypy-boto3-cloudformation provides type annotations for the boto3 CloudFormation service. It is automatically generated by mypy-boto3-builder and closely follows boto3's versioning, offering up-to-date type stubs for improved static analysis and IDE assistance when working with AWS CloudFormation using boto3. The library is actively maintained with frequent updates reflecting changes in boto3 and the AWS API.
Warnings
- breaking Python 3.8 support was removed for all mypy-boto3 packages, including mypy-boto3-cloudformation. Projects requiring type stubs must use Python 3.9 or newer.
- breaking All mypy-boto3 packages migrated to PEP 561-compliant distribution. This change primarily affects how type checkers locate stubs, potentially requiring adjustments in build configurations or explicit stub path declarations if you were relying on non-standard stub discovery methods.
- breaking TypeDef naming conventions were changed for packed method arguments, shortening names (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`). Conflicting TypeDef `Extra` postfixes were moved (e.g., `CreateDistributionExtraRequestTypeDef` to `CreateDistributionRequestExtraTypeDef`).
- gotcha This library provides type stubs for `boto3`, not the `boto3` runtime itself. You must install `boto3` separately for your code to actually run. Without `boto3`, `mypy-boto3-cloudformation` offers no functional benefit beyond type checking for a non-existent runtime.
- gotcha For optimal compatibility and accurate type hints, the major.minor version of `mypy-boto3-cloudformation` (e.g., 1.42.x) should ideally align with the major.minor version of your installed `boto3` (e.g., 1.42.x). Mismatches can lead to type checking errors or missing attributes if the underlying API changes.
Install
-
pip install boto3 mypy-boto3-cloudformation
Imports
- CloudFormationClient
from mypy_boto3_cloudformation.client import CloudFormationClient
- CloudFormationServiceName
from mypy_boto3_cloudformation import CloudFormationServiceName
- StackSummaryTypeDef
from mypy_boto3_cloudformation.type_defs import StackSummaryTypeDef
Quickstart
import boto3
from mypy_boto3_cloudformation.client import CloudFormationClient
from mypy_boto3_cloudformation.type_defs import StackSummaryTypeDef
from mypy_boto3_cloudformation import CloudFormationServiceName
# Get a typed CloudFormation client using CloudFormationServiceName for safety
client: CloudFormationClient = boto3.client(CloudFormationServiceName.CLOUDFORMATION)
# Example: List stacks and iterate with type hints
try:
response = client.list_stacks(StackStatusFilter=['CREATE_COMPLETE', 'UPDATE_COMPLETE'])
# response['StackSummaries'] is a list of StackSummaryTypeDef
stacks: list[StackSummaryTypeDef] = response.get('StackSummaries', [])
if stacks:
print("CloudFormation Stacks:")
for stack in stacks:
print(f" Name: {stack['StackName']}, Status: {stack['StackStatus']}, Id: {stack['StackId']}")
else:
print("No CREATE_COMPLETE or UPDATE_COMPLETE stacks found.")
except Exception as e:
print(f"Error listing stacks: {e}")
# The type checker (e.g., mypy) will now validate usage of `client`
# and `stack` objects against the CloudFormation API definition.