mypy-boto3-lambda Type Annotations
mypy-boto3-lambda provides type annotations for the AWS Lambda client in `boto3`, generated by `mypy-boto3-builder`. It enhances type checking for `boto3` usage, preventing common runtime errors related to incorrect AWS service calls or response parsing. The library version `1.42.85` corresponds to `boto3` version `1.42.85`. New versions are released frequently, typically mirroring `boto3` releases.
Warnings
- breaking Support for Python 3.8 was removed. Packages generated by `mypy-boto3-builder` v8.12.0 and later (which includes `mypy-boto3-lambda` v1.42.85) require Python 3.9 or higher.
- breaking The `sms-voice` service was deprecated and renamed to `pinpoint-sms-voice`. Direct usage of the `sms-voice` client will fail.
- breaking TypeDef names for packed method arguments changed, becoming shorter (e.g., `CreateDistributionRequestRequestTypeDef` -> `CreateDistributionRequestTypeDef`). Conflicting TypeDef `Extra` postfixes also moved.
- gotcha This package provides only type stubs. It is not the `boto3` library itself. You must install `boto3` separately for your code to function at runtime.
- gotcha The `mypy-boto3-*` stub packages are versioned to align with specific `boto3` versions. Using mismatched versions (e.g., `mypy-boto3-lambda` 1.42.85 with `boto3` 1.20.0) can lead to incorrect type hints or `mypy` errors.
Install
-
pip install mypy-boto3-lambda boto3
Imports
- LambdaClient
from mypy_boto3_lambda.client import LambdaClient
- ListFunctionsResponseTypeDef
from mypy_boto3_lambda.type_defs import ListFunctionsResponseTypeDef
Quickstart
import boto3
from mypy_boto3_lambda.client import LambdaClient
from mypy_boto3_lambda.type_defs import ListFunctionsResponseTypeDef
# Create a typed Lambda client
# mypy will automatically use the installed mypy-boto3-lambda stubs for type checking
lambda_client: LambdaClient = boto3.client("lambda")
try:
# Example: List Lambda functions
# The response object will be correctly typed
response: ListFunctionsResponseTypeDef = lambda_client.list_functions(MaxItems=1)
print("Successfully listed Lambda functions (first item): ")
for func in response.get("Functions", []):
print(f"- {func['FunctionName']}")
except Exception as e:
# This may happen if AWS credentials are not configured or if the user lacks permissions.
print(f"Error listing Lambda functions: {e}")
print("Ensure your AWS credentials are configured (e.g., via AWS CLI or environment variables).")