mypy-boto3-kms Type Stubs for AWS KMS
mypy-boto3-kms provides static type annotations for the AWS Key Management Service (KMS) client in `boto3`, enhancing type safety and developer experience with tools like MyPy. It is part of the `mypy-boto3` collection, automatically generated by `mypy-boto3-builder`, and its current version (1.42.50) is synchronized with the underlying `boto3` service definition versions. The project maintains a regular release cadence, often updating with new `boto3` versions and `mypy-boto3-builder` enhancements.
Warnings
- breaking As of `mypy-boto3-builder` version 8.12.0, Python 3.8 support has been removed across all generated `mypy-boto3` packages. You must use Python 3.9 or newer.
- breaking TypeDef naming conventions were significantly changed in `mypy-boto3-builder` 8.9.0. TypeDefs for packed method arguments now use shorter names (e.g., `CreateDistributionRequestRequestTypeDef` -> `CreateDistributionRequestTypeDef`), and conflicting 'Extra' postfixes moved to the end. Code relying on specific TypeDef names may break.
- gotcha `mypy-boto3-kms` provides only type stubs. For your application to run, `boto3` must be installed and properly configured (e.g., AWS credentials) in your environment. These stubs do not provide any runtime functionality.
- gotcha The version of `mypy-boto3-kms` should ideally align with the `boto3` version you are using or the `boto3` service definition it was generated against. Mismatches can lead to incorrect type hints for newer or older API features.
- gotcha The `mypy-boto3` packages migrated to PEP 561 compatible package structure in `mypy-boto3-builder` 8.12.0. If you have custom `mypy` configurations that rely on specific internal paths or non-PEP 561 layouts, you may need to update them.
Install
-
pip install boto3 mypy-boto3-kms
Imports
- KMSClient
from mypy_boto3_kms.client import KMSClient
- CreateKeyRequestRequestTypeDef
from mypy_boto3_kms.type_defs import CreateKeyRequestRequestTypeDef
- ListKeysResponseTypeDef
from mypy_boto3_kms.type_defs import ListKeysResponseTypeDef
Quickstart
import boto3
from mypy_boto3_kms.client import KMSClient
from mypy_boto3_kms.type_defs import ListKeysResponseTypeDef, CreateKeyRequestRequestTypeDef
# Initialize a KMS client, type-hinted for static analysis
kms_client: KMSClient = boto3.client("kms")
# Example operation: List KMS keys
try:
# mypy will verify the return type matches ListKeysResponseTypeDef
response: ListKeysResponseTypeDef = kms_client.list_keys()
print("KMS Keys:")
for key in response.get("Keys", []):
print(f" Key ID: {key.get('KeyId')}, Key ARN: {key.get('KeyArn')}")
except Exception as e:
print(f"Error listing keys: {e}")
# Example of using a TypeDef for a request payload (for type-checking)
create_key_params: CreateKeyRequestRequestTypeDef = {
"Description": "My Test Key",
"KeyUsage": "ENCRYPT_DECRYPT",
"KeySpec": "SYMMETRIC_DEFAULT",
"Tags": [
{"TagKey": "Project", "TagValue": "MyProject"}
]
}
# mypy would check 'create_key_params' against 'CreateKeyRequestRequestTypeDef'
print(f"\nExample CreateKeyRequest parameters (for type-checking): {create_key_params}")
# To run mypy against this file:
# mypy your_script_name.py