mypy-boto3-personalize: Type Annotations for AWS Personalize
mypy-boto3-personalize provides comprehensive type annotations for the AWS Personalize service in `boto3`. It enhances static analysis with tools like MyPy, catching potential type-related errors before runtime. The current version is 1.42.3, tracking `boto3` releases, and is part of the frequently updated `mypy-boto3-builder` ecosystem.
Warnings
- breaking Starting with `mypy-boto3-builder` 8.12.0, Python 3.8 is no longer supported across all `mypy-boto3` packages, including `mypy-boto3-personalize`. Users on Python 3.8 must upgrade to Python 3.9+ or pin an older version of `mypy-boto3-personalize`.
- breaking TypeDef naming conventions changed in `mypy-boto3-builder` 8.9.0. Specifically, `RequestTypeDef` for method arguments might be shortened (e.g., `CreateDistributionRequestRequestTypeDef` -> `CreateDistributionRequestTypeDef`), and `Extra` postfixes were moved (e.g., `CreateDistributionExtraRequestTypeDef` -> `CreateDistributionRequestExtraTypeDef`). This can break explicit type hints for these TypeDefs.
- gotcha `mypy-boto3-personalize` provides only type stubs, not the actual runtime library. You *must* install `boto3` separately for your code to run, and `mypy` to perform static analysis. Not installing `boto3` will lead to runtime `ModuleNotFoundError`.
- gotcha While `mypy-boto3` packages are PEP 561 compliant, `mypy` might not always automatically discover stubs in complex project setups or older `mypy` versions. Ensure `mypy` is correctly configured to find the installed type packages.
Install
-
pip install mypy-boto3-personalize boto3 mypy
Imports
- PersonalizeClient
from mypy_boto3_personalize.client import PersonalizeClient
- Client
from mypy_boto3_personalize.client import Client
- CreateDatasetRequestRequestTypeDef
from mypy_boto3_personalize.type_defs import CreateDatasetRequestRequestTypeDef
- CreateDatasetResponseTypeDef
from mypy_boto3_personalize.type_defs import CreateDatasetResponseTypeDef
Quickstart
import boto3
from mypy_boto3_personalize.client import PersonalizeClient
# mypy-boto3-personalize provides type hints for the boto3 client.
# You still use the standard boto3 client at runtime.
# Configure AWS credentials as usual (e.g., ~/.aws/credentials, environment variables)
# Instantiate a boto3 client and apply the type hint
personalize_client: PersonalizeClient = boto3.client("personalize")
print(f"Client type successfully inferred by mypy-boto3-personalize: {type(personalize_client)}")
# Example of how type hints are used for method calls (for static analysis).
# This code block is for demonstration of type-checking, not intended for live execution
# without proper setup and AWS resources.
# from mypy_boto3_personalize.type_defs import CreateDatasetRequestRequestTypeDef
# try:
# # Type checkers like MyPy will ensure 'name', 'datasetGroupArn', etc. are correct types
# # and that the return value matches CreateDatasetResponseTypeDef.
# create_request: CreateDatasetRequestRequestTypeDef = {
# "name": "MyNewDataset",
# "datasetGroupArn": "arn:aws:personalize:us-east-1:123456789012:dataset-group/my-group",
# "datasetType": "ITEMS",
# "schemaArn": "arn:aws:personalize:us-east-1:123456789012:schema/my-schema",
# }
# response = personalize_client.create_dataset(**create_request)
# print(f"Type-checked call example response: {response}")
# except Exception as e:
# print(f"Example call failed (expected without real AWS setup): {e}")