mypy-boto3-xray

1.42.3 · active · verified Thu Apr 09

mypy-boto3-xray provides type annotations (stubs) for the `boto3` AWS SDK specifically for the XRay service. It enables static type checking with tools like MyPy, improving code quality and catching errors early when working with AWS XRay. The current version is 1.42.3, which is built with `mypy-boto3-builder` version 8.12.0, and new versions are released frequently to keep pace with `boto3` updates.

Warnings

Install

Imports

Quickstart

Demonstrates how to use `mypy-boto3-xray` to add type hints to a `boto3` XRay client, allowing static analysis to verify method calls and response structures.

import boto3
from mypy_boto3_xray.client import XRayClient
from mypy_boto3_xray.type_defs import GetTraceSummariesResponseTypeDef
from datetime import datetime, timedelta

# mypy-boto3-xray provides type hints for boto3, 
# so boto3 itself must be installed and used.
client: XRayClient = boto3.client("xray")

end_time = datetime.now()
start_time = end_time - timedelta(hours=1)

try:
    response: GetTraceSummariesResponseTypeDef = client.get_trace_summaries(
        StartTime=start_time,
        EndTime=end_time,
        SamplingStrategy={
            "StrategyName": "PartialScan",
            "Value": 5.0
        }
    )

    print(f"Traces found: {len(response.get('TraceSummaries', []))}")
    if response.get('TraceSummaries'):
        for trace in response['TraceSummaries']:
            print(f"  Trace ID: {trace['Id']}, Duration: {trace.get('Duration')}")
except client.exceptions.InvalidRequestException as e:
    print(f"Error fetching traces: {e}. Check your AWS region and permissions.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →