Type annotations for boto3 EMR

1.42.77 · active · verified Fri Apr 10

mypy-boto3-emr provides high-quality type annotations for the AWS boto3 EMR (Elastic MapReduce) service. It allows developers to leverage static type checking with tools like MyPy, improving code reliability and developer experience when working with boto3. The library is currently at version 1.42.77 and is part of the `youtype/mypy_boto3_builder` project, which generates new stub packages frequently, typically aligning with boto3/botocore releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `mypy-boto3-emr` to add type hints to your boto3 EMR client and API responses. The `TYPE_CHECKING` block ensures that stub imports are only processed by the type checker, avoiding runtime import errors if the stubs are not installed. Explicitly typing the client and response objects allows MyPy to provide comprehensive checks.

import boto3
from typing import TYPE_CHECKING, List, Dict, Any
import os

# Conditional import for type checking only
if TYPE_CHECKING:
    from mypy_boto3_emr.client import EMRClient
    from mypy_boto3_emr.type_defs import ListClustersOutputTypeDef, ClusterSummaryTypeDef

# Example function using type hints
def get_running_emr_clusters() -> List[ClusterSummaryTypeDef]:
    # The client variable is explicitly typed for MyPy
    client: EMRClient = boto3.client(
        "emr",
        region_name=os.environ.get('AWS_REGION', 'us-east-1'),
        aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID', 'DUMMY_KEY'),
        aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY', 'DUMMY_SECRET')
    )

    # The response variable is explicitly typed
    response: ListClustersOutputTypeDef = client.list_clusters(ClusterStates=['RUNNING'])

    clusters: List[ClusterSummaryTypeDef] = response.get('Clusters', [])
    print(f"Found {len(clusters)} running EMR clusters.")
    for cluster in clusters:
        print(f"  - Cluster ID: {cluster['Id']}, Name: {cluster['Name']}")
    return clusters

if __name__ == "__main__":
    # This code will run, but type hints are only checked by MyPy
    get_running_emr_clusters()

view raw JSON →