mypy-boto3-codecommit
mypy-boto3-codecommit provides type annotations for the boto3 CodeCommit service, enabling static type checking with mypy. It's automatically generated from AWS service definitions, ensuring up-to-date and accurate type hints. The current version is 1.42.3, with releases closely tracking boto3 and mypy-boto3-builder updates.
Warnings
- breaking Python 3.8 support was removed for all mypy-boto3 packages from builder version 8.12.0 onwards. If you are using an older Python version, you must pin your mypy-boto3-* packages to an earlier version.
- breaking TypeDef names for method arguments and conflicting names were refactored (e.g., `CreateDistributionRequestRequestTypeDef` to `CreateDistributionRequestTypeDef`, `CreateDistributionExtraRequestTypeDef` to `CreateDistributionRequestExtraTypeDef`). If you directly imported these `TypeDef`s, your code will break.
- gotcha mypy-boto3-codecommit provides *only* type stubs. You must also install `boto3` (and `mypy`) for your code to run and for type checking to be effective. Installing only the stubs will result in runtime `ModuleNotFoundError` for `boto3`.
- gotcha Type stubs are generated for specific boto3 versions. Mismatched `boto3` and `mypy-boto3-codecommit` versions can lead to incorrect type checking results (false positives/negatives) if AWS API definitions have changed significantly between versions.
- gotcha Forgetting to run `mypy` against your code means you won't benefit from the type checking provided by `mypy-boto3-codecommit`. The stubs do not enforce types at runtime.
Install
-
pip install boto3 mypy mypy-boto3-codecommit
Imports
- CodeCommitClient
from mypy_boto3_codecommit.client import CodeCommitClient
- CodeCommitServiceResource
from mypy_boto3_codecommit.service_resource import CodeCommitServiceResource
Quickstart
import os
import boto3
from mypy_boto3_codecommit.client import CodeCommitClient
from botocore.exceptions import ClientError
# Ensure AWS_REGION is set in environment variables or provide region_name directly
region = os.environ.get("AWS_REGION", "us-east-1")
try:
# Initialize a typed CodeCommit client
client: CodeCommitClient = boto3.client("codecommit", region_name=region)
print(f"Listing repositories in region: {region}...")
# Use a client method with type hints
response = client.list_repositories(
sortBy="repositoryName",
order="ascending"
)
repositories = response.get("repositories", [])
if repositories:
print(f"Found {len(repositories)} repositories:")
for repo in repositories[:3]: # Limit output for brevity
print(f"- {repo['repositoryName']} (ID: {repo['repositoryId']})")
else:
print("No repositories found.")
except ClientError as e:
print(f"AWS Client Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# To verify types, save this code as `app.py` and run:
# mypy app.py