mypy-boto3-polly Type Stubs

1.42.76 · active · verified Sat Apr 11

mypy-boto3-polly provides type annotations for the boto3 Polly service. It offers comprehensive type hints for Polly clients, paginators, and specific type definitions, which enhances static analysis with tools like mypy, PyCharm, and VSCode. The package is automatically generated by `mypy-boto3-builder` and is regularly updated to stay compatible with `boto3` releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `mypy-boto3-polly` type stubs with a standard boto3 client. It explicitly types the Polly client and its response, allowing type checkers to validate usage and provide intelligent autocomplete.

import boto3
from mypy_boto3_polly.client import PollyClient
from mypy_boto3_polly.type_defs import SynthesizeSpeechOutputTypeDef
import os


def synthesize_and_save(text: str, output_path: str):
    # Ensure AWS credentials are available (e.g., via environment variables or ~/.aws/credentials)
    # For local testing, ensure AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION are set.

    # Explicitly type the client for full type-checking benefits
    client: PollyClient = boto3.client("polly", region_name=os.environ.get('AWS_REGION', 'us-east-1'))

    response: SynthesizeSpeechOutputTypeDef = client.synthesize_speech(
        Text=text,
        OutputFormat='mp3',
        VoiceId='Joanna'
    )

    # In a real application, handle the 'AudioStream' which is a StreamingBody
    # For this quickstart, we'll just demonstrate the typing of the response.
    if response['AudioStream']:
        print(f"Successfully received audio stream with content type: {response['ContentType']}")
        # Example: to save to a file (simplified, error handling omitted)
        # with open(output_path, 'wb') as f:
        #     f.write(response['AudioStream'].read())
        # print(f"Audio saved to {output_path}")
    else:
        print("No audio stream received.")

# Example usage (requires AWS setup for actual execution)
# synthesize_and_save("Hello, world! This is a typed Polly client example.", "output.mp3")
print("Quickstart code demonstrates type hinting for boto3 Polly client.")

view raw JSON →