mypy-boto3-greengrass Type Stubs

1.42.3 · active · verified Sat Apr 11

mypy-boto3-greengrass provides comprehensive type annotations for the boto3 Greengrass client, service resource, paginators, and waiters. It enables static type checking for AWS Greengrass operations using mypy, enhancing code reliability and developer experience. The library is currently at version 1.42.3, generated with mypy-boto3-builder 8.12.0, and follows the boto3 release cadence for updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `mypy-boto3-greengrass` for type hinting your `boto3` Greengrass client. It highlights the use of `TYPE_CHECKING` for conditional imports, ensuring the stubs are only used during static analysis. When `mypy` processes this code, it uses `GreengrassClient` and `GetCoreDefinitionResponseTypeDef` to validate client methods and response structures. At runtime, the standard `boto3` client is used.

import boto3
import os
from typing import TYPE_CHECKING, Dict, Any

# Conditional import for type checking only to avoid runtime dependency
# if mypy-boto3-greengrass is not installed in production environments.
if TYPE_CHECKING:
    from mypy_boto3_greengrass.client import GreengrassClient
    from mypy_boto3_greengrass.type_defs import GetCoreDefinitionResponseTypeDef

def get_core_definition_name(core_definition_id: str) -> str:
    """
    Retrieves the name of a Greengrass core definition using type-hinted boto3.
    Type annotations ensure static analysis correctness.
    """
    # Initialize boto3 client. mypy-boto3 provides stubs for its methods.
    client = boto3.client("greengrass", region_name=os.environ.get("AWS_REGION", "us-east-1"))

    # Asserting the client type for static analysis with mypy.
    # At runtime, 'client' is a regular boto3 client.
    if TYPE_CHECKING:
        greengrass_client: GreengrassClient = client
        response: GetCoreDefinitionResponseTypeDef = greengrass_client.get_core_definition(
            CoreDefinitionId=core_definition_id
        )
    else:
        # Runtime execution uses the actual boto3 client without explicit type assertion.
        # This branch is taken if TYPE_CHECKING is False (e.g., in production).
        response = client.get_core_definition(CoreDefinitionId=core_definition_id)
    
    # Mypy will now correctly check access to 'Name' and its type
    return response['Name']

if __name__ == "__main__":
    # For this to make actual AWS calls, ensure AWS_ACCESS_KEY_ID,
    # AWS_SECRET_ACCESS_KEY (and optionally AWS_REGION) are set in your environment.
    # Otherwise, it will attempt a call that might fail with an AWS error.

    print("Note: This example requires AWS credentials for live execution.")
    print("   To run 'mypy' on this file, ensure 'mypy boto3 mypy-boto3-greengrass' are installed.")
    print("   Example: 'mypy your_script_name.py'")

    example_core_id = "your-greengrass-core-definition-id"

    if os.environ.get("AWS_ACCESS_KEY_ID") and os.environ.get("AWS_SECRET_ACCESS_KEY"):
        print(f"\nAttempting to get Greengrass Core Definition name for ID: {example_core_id}")
        try:
            core_name = get_core_definition_name(example_core_id)
            print(f"Retrieved Core Definition Name: {core_name}")
        except Exception as e:
            print(f"An error occurred during AWS call: {e}")
            print("Please ensure the Core Definition ID is valid and credentials are correct.")
    else:
        print("\nAWS credentials not found. Skipping live AWS call.")
        print("To test type-checking, you can run 'mypy' on this file without credentials.")

view raw JSON →