mypy-boto3-secretsmanager Type Stubs
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
- breaking As of `mypy-boto3-builder` version 8.12.0 (which generates these stubs), Python 3.8 is no longer supported. Packages built with this builder (including `mypy-boto3-secretsmanager` 1.x.x versions) require Python 3.9 or newer.
- gotcha Type stubs are designed to match specific `boto3` and `botocore` versions. Mismatched versions between `boto3` (or `botocore`) and `mypy-boto3-secretsmanager` can lead to inaccurate type hints or Mypy errors. Always try to keep them aligned.
- breaking For stubs generated by `mypy-boto3-builder` version 8.9.0 and later, some `TypeDef` names for method arguments and responses might have changed, primarily to shorten names or resolve conflicts (e.g., `CreateDistributionRequestRequestTypeDef` -> `CreateDistributionRequestTypeDef`).
- deprecated The monolithic `mypy-boto3` package (which included all service stubs) is now considered a legacy product. While it still exists, the recommended approach for modern `mypy-boto3-builder` versions is to install specific service packages like `mypy-boto3-secretsmanager` to reduce dependencies and improve build times.
Install
-
pip install mypy-boto3-secretsmanager boto3
Imports
- SecretsManagerClient
from mypy_boto3_secretsmanager import SecretsManagerClient
- GetSecretValueResponseTypeDef
from mypy_boto3_secretsmanager.type_defs import GetSecretValueResponseTypeDef
Quickstart
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.")