mypy-boto3-dynamodb Type Annotations
mypy-boto3-dynamodb provides type annotations for the `boto3` DynamoDB service client and resource, generated by `mypy-boto3-builder`. It ensures type-checking accuracy for your boto3 calls related to DynamoDB. The current version is 1.42.73 and it releases frequently, often in lockstep with boto3/botocore updates.
Warnings
- breaking Starting with version 8.12.0, mypy-boto3-builder (and thus mypy-boto3-dynamodb) removed support for Python 3.8. Ensure your project is running on Python 3.9 or higher.
- gotcha Type definition (TypeDef) names for request/response objects can change between major versions or updates to reflect AWS API changes or internal renaming conventions. For example, `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef` in version 8.9.0 (for CloudFront). This can break type hints in your code.
- gotcha AWS service names can be deprecated or renamed, leading to stubs for old service names being removed or requiring migration to new ones. For instance, `sms-voice` stubs were removed in favor of `pinpoint-sms-voice` in version 8.11.0.
- gotcha If you only need stubs for a specific AWS service like DynamoDB, install `mypy-boto3-dynamodb`. Installing the main `mypy-boto3` meta-package will install stubs for *all* AWS services, which might lead to a larger dependency footprint and potentially slower type-checking for large projects.
Install
-
pip install mypy-boto3-dynamodb -
pip install mypy-boto3[dynamodb]
Imports
- DynamoDBClient
from mypy_boto3_dynamodb.client import DynamoDBClient
- DynamoDBServiceResource
from mypy_boto3_dynamodb.service_resource import DynamoDBServiceResource
- CreateTableInputRequestTypeDef
from mypy_boto3_dynamodb.type_defs import CreateTableInputRequestTypeDef
Quickstart
import boto3
from mypy_boto3_dynamodb.client import DynamoDBClient
from mypy_boto3_dynamodb.type_defs import CreateTableInputRequestTypeDef
def create_table_example(table_name: str, region: str = 'us-east-1'):
client: DynamoDBClient = boto3.client('dynamodb', region_name=region)
table_params: CreateTableInputRequestTypeDef = {
'TableName': table_name,
'KeySchema': [
{'AttributeName': 'id', 'KeyType': 'HASH'}
],
'AttributeDefinitions': [
{'AttributeName': 'id', 'AttributeType': 'S'}
],
'BillingMode': 'PAY_PER_REQUEST'
}
print(f"Attempting to create table {table_name}...")
try:
response = client.create_table(**table_params)
print(f"Table creation initiated: {response['TableDescription']['TableArn']}")
except client.exceptions.ResourceInUseException:
print(f"Table {table_name} already exists.")
except Exception as e:
print(f"Error creating table: {e}")
if __name__ == '__main__':
# This example requires valid AWS credentials configured (e.g., via AWS CLI or environment variables)
# It's intended for demonstration; actual resource creation might incur costs.
# Create a dummy table name for testing.
test_table_name = 'MyTestTableForMypyBoto3'
create_table_example(test_table_name)
# Note: In a real scenario, you'd likely wait for the table to become active
# and then delete it after testing to avoid accumulating resources.