Botocore Stubs
Botocore Stubs provides comprehensive type annotations and code completion for the `botocore` library, the low-level interface to Amazon Web Services. This package, part of the `mypy_boto3_builder` project, is designed to enhance static type analysis with tools like MyPy and Pyright, and improve IDE auto-completion for `botocore`'s dynamically generated classes. It is actively maintained with versions aligned to corresponding `botocore` releases, with the current version being 1.42.41.
Warnings
- gotcha botocore-stubs only provides type hints for the low-level `botocore` library, not for the higher-level `boto3` library. Boto3 includes additional abstractions (e.g., `resource` objects, specific helper methods like `upload_file`) that `botocore-stubs` will not type. For `boto3` specific typing, consider `boto3-stubs` (part of the same `mypy_boto3_builder` project).
- gotcha Type stubs are only effective when used with a compatible static type checker (e.g., MyPy, Pyright) or an IDE with strong type-hinting support. Installing `botocore-stubs` alone will not provide runtime changes or visual hints without these tools.
- gotcha The version of `botocore-stubs` should ideally align with the version of your installed `botocore` library. Mismatched versions may lead to incomplete or incorrect type hints, especially for new features or deprecated APIs in `botocore`.
- breaking Version 1.29.0 removed deprecated stubs for client and endpoint_provider modules. If you were relying on type hints for previously deprecated interfaces within these modules, your type checking might now fail or report missing attributes.
- breaking Version 1.29.26 included a fix for `RuleSetStandardLibrary` naming within the `endpoint_provider` module. If your code directly referenced this internal naming for type hints before this fix, it might now cause type checker errors due to the correction.
Install
-
pip install botocore-stubs
Imports
- S3Client
from botocore.client import S3Client
- Session
from botocore.session import Session
Quickstart
import os
from botocore.session import Session
from botocore.client import S3Client # For type hinting the client
def get_s3_client() -> S3Client:
"""
Returns a typed S3 client from botocore.
botocore-stubs provides the type hints for S3Client.
"""
# Ensure AWS credentials are set up, e.g., via environment variables
# AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION
session = Session()
# The return type of session.create_client('s3') is dynamically generated.
# The type checker uses botocore-stubs to infer it as S3Client.
return session.create_client('s3', region_name=os.environ.get('AWS_REGION', 'us-east-1'))
if __name__ == "__main__":
s3_client = get_s3_client()
try:
# This call will have type hints thanks to botocore-stubs
response = s3_client.list_buckets()
print("Successfully listed S3 buckets:")
for bucket in response.get('Buckets', []):
print(f"- {bucket['Name']}")
except Exception as e:
print(f"Error listing S3 buckets: {e}")
print("Ensure you have AWS credentials configured (e.g., AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION environment variables).")