mypy-boto3-lightsail Type Stubs for AWS Lightsail

1.42.84 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to use `mypy-boto3-lightsail` to add type hints for the Lightsail client and its response types. It includes a `TYPE_CHECKING` guard for runtime compatibility and shows how to retrieve Lightsail distributions with type safety. Remember to have AWS credentials configured for actual execution.

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

view raw JSON →