Type annotations for boto3 DynamoDB
types-boto3-dynamodb provides comprehensive type annotations for the `boto3` AWS SDK's DynamoDB service. It is part of the `types-boto3` project and is generated by `mypy-boto3-builder` 8.12.0. These stubs enhance developer experience by enabling static type checking, autocompletion, and improved readability for `boto3` interactions with DynamoDB, compatible with tools like VSCode, PyCharm, mypy, and pyright. The library is actively maintained with frequent updates to align with new `boto3` versions and Python features.
Warnings
- breaking Python 3.8 support has been removed. `types-boto3-dynamodb` and its builder `mypy-boto3-builder` now require Python 3.9 or newer.
- breaking The project migrated to PEP 561 compliant packages. This might require adjustments in how some build systems or advanced type checker configurations resolve imports, though direct usage often remains compatible.
- breaking TypeDef naming conventions were changed for some packed method arguments and conflicting names (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`). If you directly reference these generated TypedDicts, your code may break.
- gotcha For optimal production environments, it is recommended to conditionally import type hints using `if TYPE_CHECKING:` to prevent adding the stub package as a runtime dependency. This avoids potential `pylint` warnings if not handled correctly.
- gotcha PyCharm users might experience slow performance and high CPU usage, especially with `Literal` overloads. This is a known issue (PY-40997) within PyCharm's type checker.
- gotcha The overarching project name for boto3 type stubs has transitioned from `boto3-stubs` to `types-boto3`. While `types-boto3-dynamodb` remains the specific package for DynamoDB, users migrating from older setups or referencing older documentation should be aware of this change.
Install
-
pip install 'types-boto3[dynamodb]' -
pip install types-boto3-dynamodb
Imports
- DynamoDBClient
from types_boto3_dynamodb import DynamoDBClient
- DynamoDBServiceResource
from types_boto3_dynamodb import DynamoDBServiceResource
- Table
from types_boto3_dynamodb.service_resource import Table
- ArchivalSummaryTypeDef
from types_boto3_dynamodb.type_defs import ArchivalSummaryTypeDef
Quickstart
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from types_boto3_dynamodb import DynamoDBClient, DynamoDBServiceResource
from types_boto3_dynamodb.service_resource import Table
def get_dynamodb_client() -> 'DynamoDBClient':
"""Returns a type-hinted DynamoDB client."""
return boto3.client('dynamodb')
def get_dynamodb_resource() -> 'DynamoDBServiceResource':
"""Returns a type-hinted DynamoDB service resource."""
return boto3.resource('dynamodb')
def get_table(table_name: str) -> 'Table':
"""Returns a type-hinted DynamoDB Table resource."""
dynamodb_resource: DynamoDBServiceResource = get_dynamodb_resource()
return dynamodb_resource.Table(table_name)
# Example usage (without actual AWS calls):
client = get_dynamodb_client()
print(f"DynamoDB client type: {type(client)}")
resource = get_dynamodb_resource()
print(f"DynamoDB resource type: {type(resource)}")
# You would typically interact with a real table here
# table = get_table("YourTableName")
# print(f"DynamoDB table type: {type(table)}")