Type Annotations for boto3 CloudTrail
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
- breaking Support for Python 3.8 and older was removed in `mypy-boto3-builder` version 8.12.0. All generated `mypy-boto3-*` packages, including `mypy-boto3-cloudtrail`, now require Python >= 3.9.
- breaking In `mypy-boto3-builder` 8.9.0, there were changes to how TypeDef names are generated, specifically for packed method arguments (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`) and conflicting `Extra` postfixes (`CreateDistributionExtraRequestTypeDef` to `CreateDistributionRequestExtraTypeDef`). While `cloudtrail` specific types might not match these exact examples, similar renamings could apply to other service `TypeDef`s.
- gotcha PyCharm users might experience slow performance with Literal overloads (issue PY-40997). The recommendation is to use `boto3-stubs-lite` (e.g., `pip install 'boto3-stubs-lite[cloudtrail]'`) or disable PyCharm's internal type checker and rely on `mypy` or `pyright`.
- gotcha When using `TYPE_CHECKING` blocks for conditional imports to avoid runtime dependencies (common for stub packages), Pylint might complain about undefined variables. To fix this, set variables inside the `TYPE_CHECKING` block to `object` in the `else` branch.
- breaking The `mypy-boto3-builder` migrated to PEP 561 compliant packages in version 8.12.0. While designed to be backward compatible for standard usage, this fundamental change in packaging type information might affect highly customized build systems or older `mypy`/`pip` versions that relied on pre-PEP 561 behavior.
Install
-
pip install mypy-boto3-cloudtrail -
pip install 'boto3-stubs[cloudtrail]'
Imports
- CloudTrailClient
from mypy_boto3_cloudtrail.client import CloudTrailClient
- LookupEventsPaginator
from mypy_boto3_cloudtrail.paginator import LookupEventsPaginator
- LookupEventsResponseTypeDef
from mypy_boto3_cloudtrail.type_defs import LookupEventsResponseTypeDef
Quickstart
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}")