mypy-boto3-codebuild Type Stubs
mypy-boto3-codebuild provides type annotations for the `boto3` CodeBuild service, enhancing static analysis with tools like MyPy. It is part of the `mypy-boto3` family of libraries, with version `1.42.3` currently available. Releases are frequent, typically mirroring `boto3` updates and `mypy-boto3-builder` changes.
Warnings
- breaking Python 3.8 support has been removed. Projects using `mypy-boto3-codebuild` or other `mypy-boto3` family stubs must upgrade to Python 3.9 or newer.
- breaking TypeDef naming conventions were changed, potentially affecting code that directly imports and references specific TypedDicts. For example, `CreateDistributionRequestRequestTypeDef` might become `CreateDistributionRequestTypeDef`.
- gotcha These type stubs are for static analysis by tools like MyPy and do not alter the runtime behavior of `boto3`. Incorrectly configured MyPy or expecting runtime changes based on stub updates can lead to confusion.
- gotcha AWS service names can change or be deprecated (e.g., `sms-voice` replaced by `pinpoint-sms-voice` in `mypy-boto3-builder` 8.11.0). This can lead to `ModuleNotFoundError` or incorrect type hints if the underlying `boto3` service name changes and the stubs are not updated accordingly.
- gotcha When using `boto3.resource` or `boto3.client`, make sure the `service_name` string argument is correctly spelled and matches the stub package. A typo will bypass type checking for that client/resource.
Install
-
pip install mypy-boto3-codebuild boto3
Imports
- CodeBuildClient
from mypy_boto3_codebuild.client import CodeBuildClient
- ProjectTypeDef
from mypy_boto3_codebuild.type_defs import ProjectTypeDef
Quickstart
import boto3
from mypy_boto3_codebuild.client import CodeBuildClient
from typing import TYPE_CHECKING
import os
from botocore.exceptions import ClientError
# Dummy variables to satisfy the instruction, though boto3 typically
# reads these implicitly if set in the environment (e.g., AWS_ACCESS_KEY_ID).
# Setting them to empty strings ensures the example runs without immediately
# failing if actual env vars are missing, but the boto3 client will still
# attempt to find valid credentials via its standard chain.
access_key_id = os.environ.get('AWS_ACCESS_KEY_ID', '')
secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY', '')
session_token = os.environ.get('AWS_SESSION_TOKEN', '')
if TYPE_CHECKING:
# These imports are only for type checking and will not be used at runtime
from mypy_boto3_codebuild.type_defs import ProjectTypeDef
try:
# Initialize a CodeBuild client with type hints.
# Boto3 will automatically use credentials from environment variables,
# shared config, or IAM roles.
# If explicit credentials were desired, one might do:
# client: CodeBuildClient = boto3.client(
# "codebuild",
# aws_access_key_id=access_key_id,
# aws_secret_access_key=secret_access_key,
# aws_session_token=session_token
# )
# But for a quickstart, implicit loading is more common and robust.
client: CodeBuildClient = boto3.client("codebuild")
# Example: List CodeBuild projects
print("Listing CodeBuild projects:")
response = client.list_projects()
projects: list[str] = response["projects"]
for project_name in projects:
print(f"- {project_name}")
# Example: Get details for a specific project (if any exist)
if projects:
project_name = projects[0]
print(f"\nGetting details for project: {project_name}")
project_details_response = client.batch_get_projects(names=[project_name])
if project_details_response and project_details_response["projects"]:
project: ProjectTypeDef = project_details_response["projects"][0]
print(f" Project ARN: {project.get('arn')}")
print(f" Project Status: {project.get('build', {}).get('status')}")
else:
print("\nNo CodeBuild projects found to display details.")
except ClientError as e:
error_code = e.response.get("Error", {}).get("Code")
if error_code in ["UnrecognizedClientException", "InvalidClientTokenId", "ExpiredToken", "NoCredentialsError"]: # Added NoCredentialsError for common boto3 errors
print(f"Authentication failed. Please ensure AWS credentials (e.g., via environment variables "
f"AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) are configured correctly. Error: {e}")
else:
print(f"An AWS client error occurred: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")