Type annotations for boto3
types-boto3 provides comprehensive type annotations (stubs) for the boto3 library, enabling static type checkers like MyPy to validate usage of AWS services. It is generated by the mypy-boto3-builder project and is currently at version 1.42.85. New versions are released frequently, typically mirroring boto3 updates or adding builder improvements.
Warnings
- breaking Python 3.8 support was removed for all `mypy-boto3-builder` generated packages, including `types-boto3`.
- breaking Some TypeDef names were changed for brevity or to resolve conflicts (e.g., `CreateDistributionRequestRequestTypeDef` to `CreateDistributionRequestTypeDef`, `CreateDistributionExtraRequestTypeDef` to `CreateDistributionRequestExtraTypeDef`).
- breaking The `sms-voice` service stubs were removed and are no longer generated. Users should migrate to `pinpoint-sms-voice`.
- gotcha Installing `types-boto3` will install stubs for *all* AWS services, leading to a larger installation size and more dependencies than often necessary.
- gotcha Ensure `boto3` is installed alongside `types-boto3`. `types-boto3` only provides type hints and does not include the runtime library itself.
Install
-
pip install types-boto3 -
pip install types-boto3-s3 types-boto3-ec2
Imports
- S3Client
from types_boto3_s3.client import S3Client
- Session
from boto3.session import Session
- BucketTypeDef
from types_boto3_s3.type_defs import BucketTypeDef
Quickstart
import boto3
from typing import TYPE_CHECKING
# It's good practice to guard stub imports for runtime environments
if TYPE_CHECKING:
from types_boto3_s3.client import S3Client
from types_boto3_s3.type_defs import BucketTypeDef
def list_s3_buckets(client: "S3Client") -> list["BucketTypeDef"]:
response = client.list_buckets()
return response.get("Buckets", [])
if __name__ == "__main__":
# In a real application, you'd configure credentials/region
# via environment variables or ~/.aws/credentials
s3_client: S3Client = boto3.client("s3")
buckets = list_s3_buckets(s3_client)
print(f"Found {len(buckets)} S3 buckets.")
for bucket in buckets:
print(f"- {bucket['Name']}")