Type annotations for boto3 KMS
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
-
Cannot find type stub for 'boto3'
cause The `types-boto3` or service-specific stub package (`types-boto3-kms`) is not installed or Mypy is not configured to find it.fixInstall the correct stub package: `pip install types-boto3-kms` (for KMS only) or `pip install 'types-boto3[all]'` (for all services). Ensure Mypy is configured correctly in your project. -
Module 'botocore.client' has no attribute 'KMS'
cause This error typically occurs when attempting to use a type hint like `botocore.client.KMS` directly, or when the type stubs are not properly recognized by the static analyzer or IDE.fixEnsure `types-boto3-kms` is installed and use the correct import path from `mypy_boto3_kms.client` within a `TYPE_CHECKING` block. Example: `if TYPE_CHECKING: from mypy_boto3_kms.client import KMSClient`. -
Argument of type 'Dict[str, Any]' cannot be assigned to parameter of type 'MyRequestTypeDef'
cause You are passing a plain Python dictionary where `mypy-boto3` expects a specific `TypedDict` from the stub package, leading to a type mismatch.fixImport the specific `TypeDef` from `mypy_boto3_kms.type_defs` (e.g., `from mypy_boto3_kms.type_defs import CreateKeyRequestTypeDef`) and explicitly construct your dictionary to match its structure.
Warnings
- breaking Python 3.8 support has been removed in `mypy-boto3-builder` version 8.12.0 (and thus `types-boto3-kms`). Projects using Python 3.8 will not receive updates or new packages.
- deprecated The legacy `mypy-boto3` package, which contained stubs for all services in one package, has been deprecated in favor of the modular `types-boto3` and service-specific packages like `types-boto3-kms`.
- gotcha Forgetting the `if TYPE_CHECKING:` guard when importing client types (e.g., `from mypy_boto3_kms.client import KMSClient`) can inadvertently make `types-boto3-kms` a runtime dependency for your application, which is unnecessary and adds overhead.
- gotcha PyCharm's performance can be slow with `Literal` overloads in `mypy-boto3` packages (issue PY-40997). This can lead to sluggish autocompletion or analysis.
Install
-
pip install types-boto3-kms boto3 -
pip install 'types-boto3[kms]' boto3
Imports
- KMSClient
from typing import TYPE_CHECKING if TYPE_CHECKING: from mypy_boto3_kms.client import KMSClient - ListKeysResponseTypeDef
from typing import TYPE_CHECKING if TYPE_CHECKING: from mypy_boto3_kms.type_defs import ListKeysResponseTypeDef
Quickstart
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())