Type annotations for boto3 Batch
mypy-boto3-batch provides comprehensive type annotations for the AWS Boto3 Batch service, generated automatically with mypy-boto3-builder. It enhances development experience by enabling static type checking with tools like MyPy, Pyright, and improving autocompletion in IDEs such as VSCode and PyCharm. The library is actively maintained, with updates released frequently to align with new Boto3 versions and address issues.
Warnings
- breaking Support for Python 3.8 has been removed across all `mypy-boto3-*` packages, starting with `mypy-boto3-builder` version 8.12.0.
- breaking TypeDef naming conventions were changed, potentially affecting custom code that directly imports and uses generated TypeDefs. For instance, `CreateDistributionRequestRequestTypeDef` might become `CreateDistributionRequestTypeDef`, and `Extra` postfixes are moved to the end.
- gotcha When using PyCharm, performance issues with `Literal` overloads have been reported (issue PY-40997). This may lead to slow performance and high CPU usage.
- gotcha For optimal IDE autocompletion and type checking, especially in VSCode without Pylance, explicit type annotations for `boto3.client()` calls are recommended, even if `boto3-stubs` provides some implicit type discovery.
- gotcha To avoid `mypy-boto3-batch` becoming a production dependency and to prevent `pylint` complaints about undefined variables when the stub package is not installed in production, it is recommended to wrap stub imports within a `if TYPE_CHECKING:` block.
Install
-
pip install boto3 mypy-boto3-batch -
pip install 'boto3-stubs[batch]'
Imports
- BatchClient
from mypy_boto3_batch.client import BatchClient
- BatchServiceResource
from mypy_boto3_batch.service_resource import BatchServiceResource
- ArrayJobDependencyType
from mypy_boto3_batch.literals import ArrayJobDependencyType
- ArrayPropertiesDetailTypeDef
from mypy_boto3_batch.type_defs import ArrayPropertiesDetailTypeDef
Quickstart
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_batch.client import BatchClient
from mypy_boto3_batch.type_defs import DescribeJobQueuesResponseTypeDef
# Example of a Paginator import
from mypy_boto3_batch.paginators import ListJobsPaginator
def get_batch_client() -> 'BatchClient':
"""Returns a type-hinted boto3 Batch client."""
return boto3.client("batch")
def describe_queues() -> 'DescribeJobQueuesResponseTypeDef':
"""Describes Batch job queues with type hints."""
client: BatchClient = get_batch_client()
response = client.describe_job_queues()
print(f"Job Queues: {len(response['jobQueues'])}")
return response
def list_all_jobs():
"""Lists all jobs using a paginator with type hints."""
client: BatchClient = get_batch_client()
paginator: ListJobsPaginator = client.get_paginator('list_jobs')
for page in paginator.paginate(jobQueue='example-queue'): # Replace 'example-queue' with an actual queue
for job in page.get('jobSummaryList', []):
print(f"Job ID: {job['jobId']}, Name: {job['jobName']}")
# Example usage (requires AWS credentials and an existing Batch setup)
if __name__ == '__main__':
# This code will execute if you have AWS credentials configured
# and an AWS Batch environment. For type checking, no actual AWS call is needed.
# To run this, ensure boto3 is configured, e.g., via AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION
# Or by using 'aws configure'
try:
queues_response = describe_queues()
# You can add more complex logic here
# list_all_jobs() # Uncomment to test paginator, requires a valid jobQueue
except Exception as e:
print(f"Error interacting with AWS Batch (this is expected if AWS credentials/resources are not set up): {e}")