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.
Common errors
-
ModuleNotFoundError: No module named 'mypy-boto3-batch'
cause The `mypy-boto3-batch` library or its parent `boto3-stubs` package, which provides type annotations for the AWS Batch service, has not been installed in your Python environment.fixInstall the library using pip: `python -m pip install mypy-boto3-batch` or `python -m pip install 'boto3-stubs[batch]'`. -
ImportError: cannot import name 'BatchClient' from 'mypy_boto3' (or similar for 'batch')
cause You are attempting to import the `BatchClient` type directly from the top-level `mypy_boto3` package or an incorrect submodule. The type annotations for specific AWS services like Batch are located in their own dedicated `mypy-boto3-<service_name>` package.fixImport `BatchClient` from the specific `mypy_boto3_batch.client` module: `from mypy_boto3_batch.client import BatchClient`. -
mypy: Unexpected keyword argument "<argument_name>" for "<method_name>" of "BatchClient"
cause Mypy cannot correctly infer the types for `boto3` client methods or their arguments when the `boto3` client is not explicitly type-hinted with the `mypy-boto3-batch` types. This often happens if the generic `boto3.client()` return type is used.fixExplicitly type-hint your Batch client with `BatchClient` from `mypy_boto3_batch.client`. For example: `from mypy_boto3_batch.client import BatchClient import boto3 def get_batch_client() -> BatchClient: return boto3.client('batch') batch_client: BatchClient = get_batch_client()`. -
mypy: Cannot find implementation or library stub for module 'boto3'
cause Mypy is unable to locate the installed `boto3-stubs` or `mypy-boto3-batch` type annotation packages. This can be due to an incorrect Mypy configuration, environment issues (e.g., virtual environment not active), or Mypy not following imports correctly.fixEnsure `boto3-stubs` (or `mypy-boto3-batch`) is installed in the environment Mypy is running in. Verify your Mypy configuration (e.g., `mypy.ini`) to ensure `mypy_path` is correctly set if needed, or temporarily remove `--ignore-missing-imports` to surface more specific errors. `python -m pip install 'boto3-stubs[batch]'`.
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.
- breaking Boto3 itself will no longer support Python 3.9 starting April 29, 2026. It is recommended to upgrade to Python 3.10 or later for continued service updates, bug fixes, and security updates.
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}")