mypy-boto3-dynamodb Type Annotations

1.42.73 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to use `mypy-boto3-dynamodb` to type-hint a `boto3` DynamoDB client and a `CreateTableInputRequestTypeDef` for creating a table. The types provide autocompletion and static analysis for your DynamoDB operations.

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.

view raw JSON →