mypy-boto3-translate Type Stubs
mypy-boto3-translate provides comprehensive type annotations for the AWS boto3 Translate service, enhancing development with static type checking. It is generated by the mypy-boto3-builder, ensuring alignment with boto3's API definitions. The current version is 1.42.3, typically updated frequently to match new boto3 releases and builder improvements.
Warnings
- breaking Starting with `mypy-boto3-builder` version 8.12.0 (which generates `mypy-boto3-translate` packages), Python 3.8 is no longer supported. The `requires_python` metadata is now `>=3.9`.
- breaking The `mypy-boto3-builder` (version 8.9.0) introduced changes to generated TypeDef naming conventions. For instance, `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`, and the `Extra` postfix for conflicting names moved to the end (e.g., `CreateDistributionExtraRequestTypeDef` -> `CreateDistributionRequestExtraTypeDef`).
- gotcha These packages provide only type stubs for `boto3`. You must install `boto3` itself separately for your application to run at runtime. `mypy-boto3-translate` does not automatically install `boto3`.
- gotcha For the most accurate type checking, it is crucial to align the version of `mypy-boto3-translate` with the version of `boto3` you are using at runtime. Mismatched versions can lead to incorrect type hints or undetected API discrepancies.
- gotcha The `mypy-boto3` ecosystem migrated to PEP 561 package format with `mypy-boto3-builder` version 8.12.0. While this is a standard for distributing type stubs, older tooling or specific `mypy` configurations might experience changes in how stubs are discovered or resolved.
Install
-
pip install mypy-boto3-translate -
pip install boto3
Imports
- TranslateClient
from mypy_boto3_translate.client import TranslateClient
- TranslateTextRequestRequestTypeDef
from mypy_boto3_translate.type_defs import TranslateTextRequestRequestTypeDef
- TranslateTextResponseTypeDef
from mypy_boto3_translate.type_defs import TranslateTextResponseTypeDef
- JobStatusType
from mypy_boto3_translate.literals import JobStatusType
Quickstart
import boto3
from typing import TYPE_CHECKING, Dict, Any
# These imports are only for type checking and are typically guarded
# to prevent runtime errors if the stub package is not installed.
if TYPE_CHECKING:
from mypy_boto3_translate.client import TranslateClient
from mypy_boto3_translate.type_defs import (
TranslateTextRequestRequestTypeDef,
TranslateTextResponseTypeDef
)
def translate_text_example(text: str, source_language: str, target_language: str) -> Dict[str, Any]:
"""Translates text using AWS Translate service with type hints."""
# Create a boto3 client. The type hint helps mypy understand its methods.
client: TranslateClient = boto3.client("translate")
# Define the request payload with a type hint for validation.
request_payload: TranslateTextRequestRequestTypeDef = {
"Text": text,
"SourceLanguageCode": source_language,
"TargetLanguageCode": target_language
# Optional parameters can also be type-hinted and added here:
# 'TerminologyNames': [],
# 'Settings': {'Formality': 'FORMAL'}
}
# Call the service method. The response is also type-hinted.
response: TranslateTextResponseTypeDef = client.translate_text(**request_payload)
return response
if __name__ == "__main__":
import os
# Ensure AWS credentials are configured (e.g., via environment variables or ~/.aws/credentials)
# For this example, we'll use dummy text. Boto3 will handle credential lookup.
# If running locally, make sure your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
# are set as environment variables or configured in ~/.aws/credentials.
try:
translated_output = translate_text_example(
text="Hello, world! How are you today?",
source_language="en",
target_language="es"
)
print(f"Original text: Hello, world! How are you today?")
print(f"Translated text (ES): {translated_output['TranslatedText']}")
# Expected output: Translated text (ES): ¡Hola, mundo! ¿Cómo estás hoy?
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure AWS credentials are configured and the Translate service is accessible.")