Type annotations for boto3 EMR
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
- breaking Support for Python 3.8 has been removed in `mypy-boto3-builder` version 8.12.0. Users on Python 3.8 will need to upgrade to Python 3.9 or newer to use the latest stub packages.
- breaking TypeDef naming conventions changed significantly in `mypy-boto3-builder` version 8.9.0. Specifically, redundant 'Request' suffixes were removed (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`), and 'Extra' postfixes moved to the end of the name. This may break explicit imports of TypeDefs.
- gotcha `mypy-boto3-emr` provides *only* type stubs for the EMR service. It does not include the runtime `boto3` library itself. For your code to run, `boto3` must be installed separately.
- gotcha AWS service names can change or be deprecated over time (e.g., `sms-voice` was replaced by `pinpoint-sms-voice` in builder 8.11.0). This means the corresponding `mypy-boto3` stub package names might also change or be removed. Always consult the latest `boto3` and `mypy-boto3` documentation for current service names.
Install
-
pip install mypy-boto3-emr -
pip install boto3 mypy
Imports
- EMRClient
from mypy_boto3_emr.client import EMRClient
- ListClustersOutputTypeDef
from mypy_boto3_emr.type_defs import ListClustersOutputTypeDef
Quickstart
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()