mypy-boto3-apigatewayv2 type annotations

1.42.76 · active · verified Sat Apr 11

mypy-boto3-apigatewayv2 provides a complete set of type annotations for the boto3 ApiGatewayV2 client and associated data structures, enabling static type checking with tools like MyPy. It ensures type safety and autocompletion for AWS SDK usage. The current version is 1.42.76, aligning with the `boto3` version it targets. New versions are released frequently in sync with `mypy-boto3-builder` updates and AWS API changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `mypy-boto3-apigatewayv2` to add type annotations to a `boto3` client. It shows both explicit type hinting for the client and for a request dictionary, which MyPy uses to provide autocompletion and detect type mismatches. Note that `boto3` must be installed alongside the stub package.

import boto3
import mypy_boto3_apigatewayv2  # Import for MyPy to discover stubs
from mypy_boto3_apigatewayv2.client import ApiGatewayV2Client
from mypy_boto3_apigatewayv2.type_defs import CreateApiRequestRequestTypeDef

# Instantiate a boto3 client at runtime. The stub package provides type annotations.
client: ApiGatewayV2Client = boto3.client("apigatewayv2")

print(f"Client type at runtime: {type(client)}")

# Example of a type-checked input dictionary for a method call
# MyPy will validate that 'create_api_params' conforms to CreateApiRequestRequestTypeDef
create_api_params: CreateApiRequestRequestTypeDef = {
    "Name": "MyExampleApiForTypes",
    "ProtocolType": "WEBSOCKET", # or 'HTTP'
    "Description": "API created for type demonstration",
    "Tags": {"Project": "ChecklistDay"}
    # Add other required parameters as per AWS API Gateway V2 documentation if performing a real call
}

# Although this call will likely fail without valid AWS credentials and full required parameters,
# it is syntactically correct and demonstrates where type-checking benefits apply.
try:
    response = client.create_api(**create_api_params)
    print(f"Attempted to create API with ID: {response.get('ApiId', 'N/A')}")
except Exception as e:
    print(f"Note: API call failed, likely due to missing AWS credentials or incomplete parameters. Error: {e}")

# MyPy would ensure that 'response' is treated as a CreateApiResultTypeDef.

view raw JSON →