mypy-boto3-ds: Type Annotations for boto3 DirectoryService

1.42.3 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to obtain a type-hinted DirectoryService client and use it to list directories. The `if TYPE_CHECKING` block ensures that the `mypy-boto3-ds` imports are only used by type checkers, preventing them from becoming runtime dependencies. It also shows how to type-hint the client object and the response structure, enabling better autocompletion and static analysis.

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.")

view raw JSON →