mypy-boto3-opensearch type stubs
mypy-boto3-opensearch provides static type annotations for the boto3 OpenSearchService client and its data structures. It's part of the `mypy-boto3` project, which programmatically generates up-to-date type stubs for all boto3 services. The library's releases closely follow boto3's release cadence, ensuring compatibility with the latest AWS SDK features. The current version is 1.42.81.
Warnings
- breaking Python 3.8 support has been removed. Projects using `mypy-boto3-opensearch` must use Python 3.9 or newer.
- breaking The `mypy-boto3` ecosystem migrated to PEP 561 compliant packages. This improves how type checkers discover stubs but might require updates to custom build systems or environments that expect older packaging structures.
- breaking Some TypeDef names were changed for improved consistency (e.g., `CreateDistributionRequestRequestTypeDef` to `CreateDistributionRequestTypeDef`). This change applies broadly across services and might affect custom type hints for OpenSearchService-related types.
- gotcha `mypy-boto3-opensearch` provides type annotations for `boto3`, it is not a replacement for `boto3` itself. You must `pip install boto3` separately.
- gotcha This library provides type *stubs*. To actually perform type checking, you need to install and run `mypy` (or another compatible type checker) on your codebase.
- gotcha The service name for OpenSearch in `boto3.client()` is `opensearch`, not `opensearchservice` or `os`.
Install
-
pip install mypy-boto3-opensearch
Imports
- OpenSearchServiceClient
from mypy_boto3_opensearch.client import OpenSearchServiceClient
- ListDomainNamesResponseTypeDef
from mypy_boto3_opensearch.type_defs import ListDomainNamesResponseTypeDef
Quickstart
import boto3
from mypy_boto3_opensearch.client import OpenSearchServiceClient
from mypy_boto3_opensearch.type_defs import ListDomainNamesResponseTypeDef
import os
# Ensure AWS credentials are configured (e.g., via environment variables or ~/.aws/credentials)
# For demonstration, using environment variables if available
aws_access_key_id = os.environ.get('AWS_ACCESS_KEY_ID', 'YOUR_ACCESS_KEY')
aws_secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY', 'YOUR_SECRET_KEY')
aws_session_token = os.environ.get('AWS_SESSION_TOKEN', None)
# Initialize the boto3 client for OpenSearchService, type-hinted for mypy-boto3 stubs
client: OpenSearchServiceClient = boto3.client(
"opensearch",
region_name="us-east-1",
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_session_token=aws_session_token
)
try:
# Use the client; type-checking will now be active when running `mypy`
response: ListDomainNamesResponseTypeDef = client.list_domain_names()
print("OpenSearch Domains:")
if response.get("DomainNames"):
for domain in response["DomainNames"]:
print(f"- {domain.get('DomainName')}")
else:
print(
"No domains found in us-east-1 region or credentials not configured correctly."
)
except Exception as e:
print(f"Error listing domains: {e}")
# To verify type checking, save this code as `your_script.py` and run `mypy your_script.py` in your terminal.