mypy-boto3-translate Type Stubs

1.42.3 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to use `mypy-boto3-translate` with `boto3` to type-hint a client and its method calls for the Translate service. It includes type hints for both the client instance and the request/response payloads, ensuring static type safety.

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

view raw JSON →