mypy-boto3-lambda Type Annotations

1.42.85 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This example demonstrates how to create a type-hinted `boto3` Lambda client. `mypy` will automatically pick up the installed stubs when `boto3.client('lambda')` is called, providing autocompletion and type validation. Explicit imports for `LambdaClient` and response `TypeDef` are used for clearer annotations.

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).")

view raw JSON →