Type Annotations for boto3 CloudTrail

1.42.3 · active · verified Sat Apr 11

mypy-boto3-cloudtrail provides comprehensive type annotations for the boto3 CloudTrail service, enhancing developer experience with static analysis tools like mypy, pyright, and various IDEs (VSCode, PyCharm). It ensures type-safe interactions with AWS CloudTrail APIs by providing precise type hints for clients, paginators, and service-specific TypedDicts. This package is generated by `mypy-boto3-builder` and is currently at version 1.42.3, following the corresponding boto3 version. New releases are frequent, typically aligning with boto3 updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a boto3 CloudTrail client and use the `mypy-boto3-cloudtrail` type hints for methods and response structures. It includes an example of looking up events and iterating through the typed response. The `TYPE_CHECKING` block ensures that these stub imports are only used by type checkers and not during runtime, making it safe for production.

import boto3
from typing import TYPE_CHECKING, List, Dict, Any

if TYPE_CHECKING:
    from mypy_boto3_cloudtrail.client import CloudTrailClient
    from mypy_boto3_cloudtrail.type_defs import EventTypeDef, LookupEventsResponseTypeDef


def list_cloudtrail_events(client: 'CloudTrailClient') -> List['EventTypeDef']:
    """Lists recent CloudTrail events with type hints."""
    # Example of a typed client call
    response: 'LookupEventsResponseTypeDef' = client.lookup_events(MaxResults=5)
    events: List['EventTypeDef'] = response.get('Events', [])
    for event in events:
        print(f"Event Name: {event.get('EventName')}, Event ID: {event.get('EventId')}")
    return events


if __name__ == "__main__":
    # In a real application, credentials would be configured via environment variables, ~/.aws/credentials, etc.
    # For this example, we assume default configuration.
    boto_session = boto3.Session()
    
    # The type hint here ensures static analysis tools understand the client's methods and return types
    cloudtrail_client: 'CloudTrailClient' = boto_session.client('cloudtrail')
    
    print("Listing CloudTrail events (first 5):")
    try:
        listed_events = list_cloudtrail_events(cloudtrail_client)
        if not listed_events:
            print("No events found.")
    except Exception as e:
        print(f"Error listing events: {e}")

view raw JSON →