mypy-boto3-greengrass Type Stubs
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
- breaking Support for Python 3.8 has been removed in `mypy-boto3-builder` version 8.12.0, affecting all generated `mypy-boto3-*` packages, including `mypy-boto3-greengrass`.
- breaking TypeDef naming conventions changed in `mypy-boto3-builder` 8.9.0. Specifically, packed method arguments use shorter names (e.g., `CreateDistributionRequestRequestTypeDef` -> `CreateDistributionRequestTypeDef`), and conflicting `Extra` postfixes are moved to the end.
- gotcha `mypy-boto3-greengrass` provides only type annotations; `boto3` itself is a separate runtime dependency and must be installed alongside these stubs for your application to function.
- gotcha To benefit from these type stubs, a static type checker like `mypy` or `pyright` must be installed and configured in your development environment.
- gotcha It is generally recommended that the major.minor version of `mypy-boto3-greengrass` matches the major.minor version of your installed `boto3` library for optimal compatibility and accuracy of type hints.
- gotcha For production deployments, consider using `if TYPE_CHECKING:` blocks for importing `mypy-boto3-*` modules to avoid adding `mypy-boto3-greengrass` as a runtime dependency. Some linters like `pylint` might complain about undefined variables without additional `else: ClientType = object` blocks.
Install
-
pip install boto3 mypy mypy-boto3-greengrass -
pip install 'boto3-stubs[greengrass]' mypy
Imports
- GreengrassClient
from mypy_boto3_greengrass.client import GreengrassClient
- GetCoreDefinitionResponseTypeDef
from mypy_boto3_greengrass.type_defs import GetCoreDefinitionResponseTypeDef
- GreengrassServiceResource
from mypy_boto3_greengrass.service_resource import GreengrassServiceResource
Quickstart
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.")