Type annotations for boto3 STS

1.42.3 · active · verified Sun Mar 29

mypy-boto3-sts provides PEP 561-compliant type annotations for the `boto3` STS (AWS Security Token Service) client, compatible with mypy, pyright, VSCode, PyCharm, and other type-checking tools. It ensures type safety and offers enhanced code completion for `boto3.client("sts")` operations. The current version, 1.42.3, aligns with boto3 1.42.3 and is generated by mypy-boto3-builder 8.12.0. The library follows the release cadence of `boto3` and its builder.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to apply `mypy-boto3-sts` type hints to a `boto3` STS client and an `assume_role` call. It shows importing the `STSClient` type for the client object and a `TypeDef` for the method's response, enabling static analysis and IDE auto-completion. Remember to configure AWS credentials for `boto3` to function.

import boto3
from mypy_boto3_sts.client import STSClient
from mypy_boto3_sts.type_defs import AssumeRoleResponseTypeDef
from os import environ

# Instantiate a boto3 session and an STS client, with explicit type hinting.
# In a real scenario, boto3 would pick up credentials from environment variables,
# shared credential files, or IAM roles.
session = boto3.Session()
client: STSClient = session.client("sts")

try:
    # Example: Assume a role with type-hinted arguments and response.
    # Replace 'YOUR_ROLE_ARN' with an actual AWS IAM Role ARN for testing.
    role_arn = environ.get("AWS_STS_ROLE_ARN", "arn:aws:iam::123456789012:role/MyTestRole")
    
    response: AssumeRoleResponseTypeDef = client.assume_role(
        RoleArn=role_arn,
        RoleSessionName="MySessionName",
        DurationSeconds=900  # Minimum duration is 900 seconds (15 minutes)
    )

    print("Successfully assumed role.")
    print(f"Assumed Role User ARN: {response['AssumedRoleUser']['Arn']}")
    print(f"Credentials Expiration: {response['Credentials']['Expiration']}")
except client.exceptions.ClientError as e:
    print(f"AWS Client Error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →