Type annotations for boto3 Step Functions
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
- breaking Support for Python 3.8 has been removed across all `mypy-boto3` packages, including `mypy-boto3-stepfunctions`.
- breaking TypeDef naming conventions changed in version 8.9.0. Packed method arguments may now use shorter names (e.g., `CreateDistributionRequestRequestTypeDef` -> `CreateDistributionRequestTypeDef`), and conflicting `Extra` postfixes are moved to the end (e.g., `CreateDistributionExtraRequestTypeDef` -> `CreateDistributionRequestExtraTypeDef`).
- gotcha PyCharm users may experience slow performance or high CPU usage due to an issue with Literal overloads (PY-40997) when using `boto3-stubs` or individual `mypy-boto3-*` packages.
- gotcha When conditionally importing `mypy-boto3` types with `if TYPE_CHECKING:` to avoid runtime dependencies, Pylint may report 'undefined variable' errors.
Install
-
pip install mypy-boto3-stepfunctions -
pip install boto3 mypy
Imports
- SFNClient
from mypy_boto3_stepfunctions.client import SFNClient
- ListStateMachinesOutputTypeDef
from mypy_boto3_stepfunctions.type_defs import ListStateMachinesOutputTypeDef
- EncryptionTypeType
from mypy_boto3_stepfunctions.literals import EncryptionTypeType
Quickstart
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).")