Type annotations for boto3 Step Functions

1.42.3 · active · verified Thu Apr 09

mypy-boto3-stepfunctions provides comprehensive type annotations for the AWS Step Functions service within `boto3`. It enhances static analysis tools like Mypy, Pyright, and IDEs (VSCode, PyCharm) by adding precise type hints to client methods, paginators, waiters, and TypeDefs. The current version is 1.42.3, actively maintained with releases frequently following `boto3` updates.

Warnings

Install

Imports

Quickstart

Demonstrates how to obtain a type-hinted Step Functions client and use it to list state machines. The `TYPE_CHECKING` block ensures type hints are available for static analysis without adding runtime dependencies if not desired. The `region_name` is explicitly set using an environment variable for clarity.

import boto3
import os
from typing import TYPE_CHECKING

# These imports are only for type checking purposes.
if TYPE_CHECKING:
    from mypy_boto3_stepfunctions.client import SFNClient
    from mypy_boto3_stepfunctions.type_defs import ListStateMachinesOutputTypeDef

# boto3 automatically picks up AWS credentials from environment variables
# (e.g., AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION) or ~/.aws/credentials.
# Ensure these are configured for a live AWS connection.

try:
    # Get the Step Functions client.
    # During static analysis (TYPE_CHECKING is True), 'sfn_client' will be inferred as SFNClient.
    # At runtime, it behaves like a standard boto3 client.
    sfn_client: "SFNClient" = boto3.client("stepfunctions", region_name=os.environ.get('AWS_REGION', 'us-east-1'))

    # Use the typed client. Mypy will validate arguments and the return type.
    response: ListStateMachinesOutputTypeDef = sfn_client.list_state_machines(maxResults=10)

    print("Successfully listed Step Functions state machines:")
    for sm in response["stateMachines"]:
        print(f"- {sm['name']} (ARN: {sm['stateMachineArn']})")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure your AWS credentials are configured and you have permissions for Step Functions (states:ListStateMachines).")

view raw JSON →