mypy-boto3-codebuild Type Stubs

1.42.3 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `boto3` CodeBuild client with type hints from `mypy-boto3-codebuild` and perform basic operations like listing projects and retrieving project details. It includes error handling for common `boto3` authentication issues, expecting AWS credentials to be configured via standard `boto3` mechanisms (environment variables, config files, IAM roles).

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}")

view raw JSON →