Type Annotations for boto3 CodeGuru Reviewer
mypy-boto3-codeguru-reviewer provides type annotations for the boto3 AWS SDK, specifically for the CodeGuru Reviewer service. It enhances static type checking with tools like mypy, improves IDE autocomplete, and helps catch potential bugs at development time. The library is currently at version 1.42.3 and is actively maintained with frequent updates following boto3 releases.
Warnings
- breaking Python 3.8 support was removed starting with `mypy-boto3-builder` version 8.12.0. `mypy-boto3-codeguru-reviewer` versions aligning with this builder version (1.42.3 and newer) will only support Python 3.9 and higher. Users on Python 3.8 must upgrade their Python environment.
- breaking TypeDef naming conventions changed in `mypy-boto3-builder` version 8.9.0. This can lead to shorter names for method argument TypeDefs (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`) and postfix changes for conflicting names. Existing type hints in your code that reference older TypeDef names may break.
- gotcha This library is a PEP 561-compliant stub-only package. It provides type information for `boto3`, but `boto3` itself must still be installed separately for the code to run. For `mypy` to properly find and utilize these type stubs, `mypy-boto3-codeguru-reviewer` (or the overarching `boto3-stubs` package) must be installed in the Python environment where `mypy` is executed.
- gotcha While `mypy-boto3-stubs` (the umbrella package) can offer some auto-discovery for `boto3.client` calls, for the most robust type checking, consistent autocomplete in IDEs, and clear code, it is generally recommended to explicitly type-hint your `boto3` client objects using imports like `from mypy_boto3_codeguru_reviewer.client import CodeGuruReviewerClient`.
Install
-
pip install boto3 mypy mypy-boto3-codeguru-reviewer
Imports
- CodeGuruReviewerClient
from mypy_boto3_codeguru_reviewer.client import CodeGuruReviewerClient
- ListRepositoryAssociationsResponseTypeDef
from mypy_boto3_codeguru_reviewer.type_defs import ListRepositoryAssociationsResponseTypeDef
- CodeGuruReviewerServiceName
from mypy_boto3_codeguru_reviewer.literals import CodeGuruReviewerServiceName
Quickstart
import boto3
from mypy_boto3_codeguru_reviewer.client import CodeGuruReviewerClient
from mypy_boto3_codeguru_reviewer.type_defs import ListRepositoryAssociationsResponseTypeDef
import os
def get_reviewer_client() -> CodeGuruReviewerClient:
"""Returns a type-hinted CodeGuruReviewer client."""
# Ensure AWS_REGION is set in environment or configure it explicitly
return boto3.client("codeguru-reviewer", region_name=os.environ.get("AWS_REGION", "us-east-1"))
def list_reviewer_associations() -> ListRepositoryAssociationsResponseTypeDef:
"""Lists CodeGuru Reviewer repository associations with type hints."""
client: CodeGuruReviewerClient = get_reviewer_client()
response: ListRepositoryAssociationsResponseTypeDef = client.list_repository_associations()
print("CodeGuru Reviewer Repository Associations:")
for association in response.get("RepositoryAssociationSummaries", []):
print(f"- {association.get('Name')} (State: {association.get('State')})")
return response
if __name__ == "__main__":
# Set dummy environment variables if not already set, for demonstration purposes.
# In a real scenario, use proper AWS credential configuration.
if not os.environ.get("AWS_ACCESS_KEY_ID"):
os.environ["AWS_ACCESS_KEY_ID"] = "TEST_KEY"
if not os.environ.get("AWS_SECRET_ACCESS_KEY"):
os.environ["AWS_SECRET_ACCESS_KEY"] = "TEST_SECRET"
if not os.environ.get("AWS_REGION"):
os.environ["AWS_REGION"] = "us-east-1"
try:
list_reviewer_associations()
except Exception as e:
# Boto3 client operations will fail if credentials are not valid,
# but mypy type checking will still work.
print(f"An error occurred during runtime: {e}")
print("Please ensure your AWS credentials and region are configured correctly for actual execution.")