mypy-boto3-ds: Type Annotations for boto3 DirectoryService
mypy-boto3-ds provides comprehensive type annotations for the `boto3` AWS SDK's DirectoryService client, enabling static type checking with tools like `mypy`. It's part of the larger `mypy-boto3` (now often referred to as `types-boto3`) project, which automatically generates stubs directly from botocore schemas. This ensures robust type-hinting, improved IDE autocompletion, and early error detection for your AWS interactions. The library is actively maintained, with new versions released in lockstep with `boto3` updates, currently at version 1.42.3.
Warnings
- breaking Support for Python 3.8 was removed in `mypy-boto3-builder` version 8.12.0 (corresponding to `mypy-boto3-ds` 1.42.3 generation). Projects using Python 3.8 or older will encounter issues.
- breaking In `mypy-boto3-builder` 8.9.0, there were breaking changes to TypeDef naming conventions, such as `CreateDistributionRequestRequestTypeDef` becoming `CreateDistributionRequestTypeDef`. While this specific example is not for DirectoryService, similar renames could affect other service TypeDefs.
- gotcha It is crucial to wrap `mypy-boto3-ds` specific imports within an `if TYPE_CHECKING:` block. Without this guard, the stub packages may become unnecessary runtime dependencies, potentially increasing deployment size or causing import errors if not properly installed in the runtime environment.
- gotcha While `mypy-boto3-ds` provides stubs for `boto3`, directly importing client objects like `from boto3.client import DirectoryServiceClient` will not work for type checking. The actual client objects are dynamically generated by `boto3` at runtime.
- gotcha The `mypy-boto3` project is now often referred to as `types-boto3` for its meta-package that can install stubs for all services. While `mypy-boto3-ds` is a specific service stub, referring to `mypy-boto3` in general discussions might lead to outdated documentation or package names.
Install
-
pip install mypy-boto3-ds boto3
Imports
- DirectoryServiceClient
from mypy_boto3_ds.client import DirectoryServiceClient
- DescribeDirectoriesResponseTypeDef
from mypy_boto3_ds.type_defs import DescribeDirectoriesResponseTypeDef
- DirectoryTypeDef
from mypy_boto3_ds.type_defs import DirectoryTypeDef
- Client Type Hinting (general)
from typing import TYPE_CHECKING if TYPE_CHECKING: from mypy_boto3_ds.client import DirectoryServiceClient
Quickstart
import boto3
from typing import TYPE_CHECKING, List, Dict, Any
# Using TYPE_CHECKING to ensure type imports are only for static analysis
if TYPE_CHECKING:
from mypy_boto3_ds.client import DirectoryServiceClient
from mypy_boto3_ds.type_defs import DirectoryTypeDef, DescribeDirectoriesResultTypeDef
def get_ds_client() -> "DirectoryServiceClient":
"""Returns a type-hinted DirectoryService client."""
return boto3.client("ds")
def list_all_directories() -> List["DirectoryTypeDef"]:
"""Lists all Directory Service directories with type hints."""
client: "DirectoryServiceClient" = get_ds_client()
response: "DescribeDirectoriesResultTypeDef" = client.describe_directories()
# Accessing 'DirectoryDescriptions' is now type-checked
return response.get("DirectoryDescriptions", [])
if __name__ == "__main__":
print("Attempting to list Directory Service directories...")
try:
directories = list_all_directories()
if directories:
print(f"Found {len(directories)} directories:")
for directory in directories:
# Dictionary access is also type-checked with TypedDicts
print(f"- {directory['Name']} ({directory['DirectoryId']}) - {directory['Type']}")
else:
print("No Directory Service directories found.")
except Exception as e:
print(f"Error listing directories: {e}")
print("Ensure you have AWS credentials configured (e.g., via environment variables, ~/.aws/credentials) and sufficient permissions.")