mypy-boto3-lightsail Type Stubs for AWS Lightsail
mypy-boto3-lightsail provides type annotations (stubs) for the AWS Lightsail service within the `boto3` library. It enables static type checking with tools like MyPy, enhancing code quality, readability, and error detection for `boto3` interactions before runtime. The library is actively maintained, with frequent releases often synchronized with `boto3` updates, and is currently at version 1.42.84.
Warnings
- breaking Python 3.8 support has been removed for `mypy-boto3-builder` (which generates `mypy-boto3-lightsail`) and its generated packages.
- breaking The naming convention for `TypeDef` objects was changed, potentially leading to import or name resolution errors if you explicitly imported them.
- gotcha Installing `mypy-boto3-lightsail` only provides type stubs. The actual `boto3` library is still required at runtime to interact with AWS services.
- gotcha For comprehensive type checking across multiple AWS services, consider installing `boto3-stubs[full]` or specific `boto3-stubs` extras (e.g., `boto3-stubs[lightsail]`) instead of individual `mypy-boto3-*` packages, as `mypy-boto3-lightsail` only covers the Lightsail service.
- gotcha When using Pylint with type hints inside `if TYPE_CHECKING:` blocks, Pylint might report `undefined variable` errors at runtime. This is due to Pylint not always correctly interpreting the `TYPE_CHECKING` guard.
Install
-
pip install mypy-boto3-lightsail boto3 mypy
Imports
- LightsailClient
from mypy_boto3_lightsail.client import LightsailClient
- CreateDistributionRequestTypeDef
from mypy_boto3_lightsail.type_defs import CreateDistributionRequestTypeDef
- boto3.client('lightsail')
Quickstart
import boto3
from typing import TYPE_CHECKING
from mypy_boto3_lightsail.client import LightsailClient
from mypy_boto3_lightsail.type_defs import GetDistributionsResultTypeDef
import os
# Ensure boto3 is installed: pip install boto3 mypy-boto3-lightsail
if TYPE_CHECKING:
# This block is only processed by type checkers
client: LightsailClient = boto3.client("lightsail")
else:
# This block runs at runtime
client = boto3.client("lightsail")
def get_lightsail_distributions() -> GetDistributionsResultTypeDef:
try:
response = client.get_distributions()
print(f"Successfully retrieved {len(response.get('distributions', []))} distributions.")
return response
except Exception as e:
print(f"Error retrieving Lightsail distributions: {e}")
raise
if __name__ == "__main__":
# Make sure your AWS credentials are configured (e.g., via AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION_NAME env vars)
# For a quick local test without actual AWS interaction, you can mock boto3 or ensure credentials are set.
# Example of calling the function:
# distributions = get_lightsail_distributions()
# if distributions:
# for dist in distributions.get('distributions', []):
# print(f" Distribution: {dist.get('name')}, Status: {dist.get('status')}")
print("Quickstart example set up. Run `mypy <filename>.py` for type checking.")
print("To execute, uncomment `get_lightsail_distributions()` call and ensure AWS credentials are set.")