mypy-boto3-s3 Type Annotations for AWS S3

1.42.67 · active · verified Sat Mar 28

mypy-boto3-s3 provides comprehensive type annotations for the `boto3` AWS S3 service, enhancing type checking, code completion, and error detection in Python projects. It is generated by `mypy-boto3-builder`, ensuring compatibility with popular IDEs (VSCode, PyCharm) and type checkers (mypy, pyright). The library version typically mirrors the corresponding `boto3` version, indicating active and frequent releases in sync with AWS SDK updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to obtain type-hinted S3 clients and service resources using `mypy-boto3-s3`. Explicit type annotations for `boto3.client('s3')` and `boto3.resource('s3')` are provided by importing `S3Client` and `S3ServiceResource`. This enables full code completion and type checking for S3 operations.

import boto3
from mypy_boto3_s3.client import S3Client
from mypy_boto3_s3.service_resource import S3ServiceResource, Bucket

def get_s3_client() -> S3Client:
    """Returns a type-hinted S3 client."""
    # Type is automatically discovered by mypy and IDEs (if configured)
    return boto3.client("s3")

def get_s3_resource() -> S3ServiceResource:
    """Returns a type-hinted S3 service resource."""
    # Type is automatically discovered by mypy and IDEs (if configured)
    return boto3.resource("s3")

def get_s3_bucket(bucket_name: str) -> Bucket:
    """Returns a type-hinted S3 Bucket resource."""
    s3_resource: S3ServiceResource = boto3.resource("s3")
    return s3_resource.Bucket(bucket_name)

if __name__ == "__main__":
    s3_client = get_s3_client()
    print(f"S3 Client type: {type(s3_client)}")
    # Example usage: list buckets
    try:
        response = s3_client.list_buckets()
        print("Buckets:")
        for bucket in response.get("Buckets", []):
            print(f"  - {bucket['Name']}")
    except Exception as e:
        print(f"Error listing buckets: {e}")

    # Example resource usage
    # s3_resource = get_s3_resource()
    # my_bucket = get_s3_bucket("your-bucket-name")
    # print(f"Bucket name: {my_bucket.name}")

view raw JSON →