mypy-boto3-apigatewayv2 type annotations
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
- breaking Python 3.8 support was removed from `mypy-boto3-builder` starting with version 8.12.0. Consequently, stub packages generated by this builder version (and newer) require Python 3.9 or higher.
- breaking TypeDef naming conventions changed in `mypy-boto3-builder` 8.9.0, potentially affecting custom TypeDef names. For instance, `CreateDistributionRequestRequestTypeDef` might become `CreateDistributionRequestTypeDef`.
- gotcha The `mypy-boto3-apigatewayv2` package provides only type stubs, not the actual runtime implementation of the AWS SDK. `boto3` must be installed separately for your application to function.
- gotcha Type stubs are not meant for direct instantiation. For example, do not attempt to call `mypy_boto3_apigatewayv2.client.ApiGatewayV2Client()` directly. Instead, use `boto3.client('apigatewayv2')` and let MyPy infer or be guided by the imported types.
Install
-
pip install boto3 mypy-boto3-apigatewayv2 mypy
Imports
- ApiGatewayV2Client
from mypy_boto3_apigatewayv2.client import ApiGatewayV2Client
- CreateApiRequestRequestTypeDef
from mypy_boto3_apigatewayv2.type_defs import CreateApiRequestRequestTypeDef
- mypy_boto3_apigatewayv2
import mypy_boto3_apigatewayv2
Quickstart
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.