mypy-boto3-secretsmanager Type Stubs

1.42.8 · active · verified Thu Apr 09

mypy-boto3-secretsmanager provides type annotations (stub files) for the boto3 AWS Secrets Manager service, enhancing static analysis and IDE autocompletion for Python projects. It is part of the larger `mypy-boto3` family, generated by `mypy-boto3-builder`. The package version (currently 1.42.8) typically aligns with the underlying `boto3` and `botocore` versions it provides stubs for. New versions are released frequently, reflecting updates in AWS services and `boto3` itself.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the type stubs for AWS Secrets Manager. It initializes a `SecretsManagerClient` and calls `get_secret_value`, with full type hinting for the client and the response dictionary. This allows static type checkers like Mypy to validate your code and provides rich autocompletion in IDEs.

import boto3
import os

from mypy_boto3_secretsmanager import SecretsManagerClient
from mypy_boto3_secretsmanager.type_defs import GetSecretValueResponseTypeDef


def get_secret_value_typed(secret_id: str) -> str:
    client: SecretsManagerClient = boto3.client("secretsmanager")
    try:
        response: GetSecretValueResponseTypeDef = client.get_secret_value(SecretId=secret_id)
        secret_string = response.get("SecretString")
        if secret_string is None:
            raise ValueError(f"Secret '{secret_id}' has no SecretString.")
        return secret_string
    except client.exceptions.ResourceNotFoundException:
        print(f"Secret '{secret_id}' not found.")
        return ""
    except Exception as e:
        print(f"Error retrieving secret '{secret_id}': {e}")
        return ""


if __name__ == "__main__":
    # Replace with a real secret ID or use an environment variable
    # Ensure AWS credentials are configured (e.g., via AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION env vars or ~/.aws/credentials)
    test_secret_id = os.environ.get('TEST_SECRET_ID', 'my-test-secret-id-nonexistent')
    print(f"Attempting to retrieve secret: {test_secret_id}")
    secret = get_secret_value_typed(test_secret_id)
    if secret:
        print(f"Retrieved secret (first 10 chars): {secret[:10]}...")
    else:
        print("Secret retrieval failed or secret not found.")

view raw JSON →