Type Annotations for boto3 DirectoryServiceData
mypy-boto3-ds-data provides type annotations for the `boto3` DirectoryServiceData service, enabling robust static type checking with tools like Mypy. It is part of the `mypy-boto3` project, which generates comprehensive stubs for all AWS services based on their API definitions. The current version, 1.42.3, is designed to align with recent `boto3` releases and receives frequent updates.
Warnings
- breaking mypy-boto3-builder 8.12.0 removed support for Python 3.8. Projects relying on older Python versions for stubs will need to use an older builder version or upgrade Python.
- gotcha This package (`mypy-boto3-ds-data`) provides only type stubs. You must install `boto3` separately to have the actual runtime functionality.
- gotcha Type stubs are generated for specific `boto3` versions. Using `mypy-boto3-ds-data` with a significantly mismatched `boto3` version can lead to incorrect or incomplete type checking, as API definitions may differ.
- gotcha When type-hinting a boto3 client or resource, the specific type definitions (e.g., `DirectoryServiceDataClient`) are imported from the `mypy_boto3_SERVICE_NAME` package, not directly from `boto3`.
Install
-
pip install mypy-boto3-ds-data -
pip install boto3
Imports
- DirectoryServiceDataClient
from mypy_boto3_ds_data.client import DirectoryServiceDataClient
- ListTagsForResourceRequestRequestTypeDef
from mypy_boto3_ds_data.type_defs import ListTagsForResourceRequestRequestTypeDef
Quickstart
import boto3
from mypy_boto3_ds_data.client import DirectoryServiceDataClient
from mypy_boto3_ds_data.type_defs import ListTagsForResourceRequestRequestTypeDef
import os
def get_directory_service_data_client() -> DirectoryServiceDataClient:
"""Initializes and returns a type-hinted DirectoryServiceData client."""
# Actual credentials will come from environment variables or AWS config
# os.environ.get('AWS_ACCESS_KEY_ID', ''), etc.
return boto3.client("ds-data")
client: DirectoryServiceDataClient = get_directory_service_data_client()
# Example usage with type-checked parameters
# Replace 'd-xxxxxxxxxx' with an actual Directory ID in your AWS account
# or handle if the resource might not exist.
resource_id = os.environ.get('DS_DATA_RESOURCE_ID', 'd-xxxxxxxxxx') # Placeholder
try:
params: ListTagsForResourceRequestRequestTypeDef = {
"ResourceId": resource_id
}
response = client.list_tags_for_resource(**params)
print("Tags for resource:")
for tag in response.get('Tags', []):
print(f" {tag['Key']}: {tag['Value']}")
except client.exceptions.ResourceNotFoundException:
print(f"Resource ID '{resource_id}' not found. Please provide a valid Directory ID.")
except Exception as e:
print(f"An error occurred: {e}")