mypy-boto3-xray
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
- breaking Starting with `mypy-boto3-builder` 8.12.0 (which generated `mypy-boto3-xray` 1.42.x), support for Python 3.8 has been removed across all packages. Ensure your project uses Python 3.9 or newer.
- gotcha `mypy-boto3-xray` provides *only type stubs* for static analysis. It does not include the `boto3` runtime itself. You must explicitly install `boto3` (e.g., `pip install boto3`) for your code to execute.
- breaking Starting with `mypy-boto3-builder` 8.12.0, all packages (including `mypy-boto3-xray`) migrated to PEP 561 namespace packages. While this is primarily an internal change, it might affect custom build systems or type checker configurations that rely on specific package structures.
- breaking In `mypy-boto3-builder` 8.9.0, there were breaking changes to TypeDef naming conventions (e.g., `CreateDistributionRequestRequestTypeDef` -> `CreateDistributionRequestTypeDef`). While not specific to XRay, be aware that similar changes could occur for other services.
Install
-
pip install boto3 mypy-boto3-xray
Imports
- XRayClient
from mypy_boto3_xray.client import XRayClient
- GetTraceSummariesResponseTypeDef
from mypy_boto3_xray.type_defs import GetTraceSummariesResponseTypeDef
Quickstart
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}")