Type annotations for boto3 KMS

1.42.50 · active · verified Thu Apr 16

types-boto3-kms provides static type annotations for the `boto3` AWS SDK's Key Management Service (KMS) client, compatible with static analysis tools like Mypy, Pyright, and IDEs such as VSCode and PyCharm. It enhances `boto3` code with autocompletion, type checking, and early error detection for KMS operations. This package is part of the `mypy-boto3-builder` ecosystem, currently at version 1.42.50, and releases are frequently updated to match `boto3` versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `types-boto3-kms` to add type hints to a `boto3` KMS client. It shows how to import and apply type annotations for the client object and its response structures, enabling static analysis and IDE assistance for KMS API calls. Requires AWS credentials configured to execute.

from typing import TYPE_CHECKING, List
import boto3

if TYPE_CHECKING:
    from mypy_boto3_kms.client import KMSClient
    from mypy_boto3_kms.type_defs import KeyListEntryTypeDef, ListKeysResponseTypeDef

def get_kms_key_ids() -> List[str]:
    """Lists all KMS key IDs in the current AWS account."""
    kms_client: KMSClient = boto3.client("kms")
    
    # The response object is type-hinted for better autocompletion and error checking
    response: ListKeysResponseTypeDef = kms_client.list_keys()
    
    key_ids: List[str] = []
    for key_entry in response.get("Keys", []):
        # key_entry is inferred as KeyListEntryTypeDef
        if 'KeyId' in key_entry:
            key_ids.append(key_entry['KeyId'])
            
    return key_ids

# To run this function, ensure AWS credentials are configured (e.g., via environment variables, AWS CLI, or IAM role).
# print(get_kms_key_ids())

view raw JSON →