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.
Common errors
-
ModuleNotFoundError: No module named 'mypy_boto3_secretsmanager'
cause The `mypy-boto3-secretsmanager` package, or the umbrella `boto3-stubs` package, has not been installed in your Python environment.fixInstall the package using pip: `python -m pip install mypy-boto3-secretsmanager` or `python -m pip install 'boto3-stubs[secretsmanager]'` -
error: Library stubs not installed for "boto3" (or incompatible with your Python version) / error: Skipping analyzing "boto3": module is installed, but missing library stubs or py.typed marker
cause Mypy cannot find or properly utilize the type stubs for the underlying `boto3` library, even if `mypy-boto3-secretsmanager` is installed. This can happen due to an incomplete installation, mismatched versions between `boto3` and its stubs, or mypy not being run in the correct environment.fixEnsure `boto3-stubs` (or the service-specific stub) is correctly installed for your `boto3` version: `python -m pip install 'boto3-stubs[secretsmanager]' --upgrade`. Also, confirm mypy is running in the same Python environment where stubs are installed. -
error: "SecretsManagerClient" has no attribute "some_method"; maybe "another_method"?
cause This type-checking error typically occurs when the `mypy-boto3-secretsmanager` stubs' version does not align with your installed `boto3` version, or you are trying to use a method that does not exist in the Secrets Manager client for that `boto3` version.fixEnsure that your `mypy-boto3-secretsmanager` (or `boto3-stubs`) package version is compatible with your `boto3` version. It's often recommended to install `boto3-stubs` with a version constraint matching your `boto3` installation, e.g., `python -m pip install 'boto3-stubs[secretsmanager]==X.Y.Z'` where `X.Y.Z` is your `boto3` version. -
error: Name "mypy_boto3_secretsmanager.type_defs.SecretValueEntryTypeDef" is not defined
cause The specific `TypeDef` or other type definition is being imported incorrectly, or it may not exist with that exact name in the installed version of `mypy-boto3-secretsmanager`. This often happens due to renaming in newer versions of the builder or incorrect module path.fixVerify the correct import path and name for the `TypeDef` in the `mypy-boto3-secretsmanager` documentation for your installed version. Type definitions are typically found in `mypy_boto3_secretsmanager.type_defs`. For example, `from mypy_boto3_secretsmanager.type_defs import APIErrorTypeTypeDef`.
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.
- gotcha `boto3` clients require an AWS region to be specified for successful operation. This can be done explicitly in the `boto3.client()` call, via environment variables (e.g., `AWS_REGION` or `AWS_DEFAULT_REGION`), or through AWS configuration files. Failure to specify a region results in a `botocore.exceptions.NoRegionError`.
- gotcha The `boto3` client failed to initialize because no AWS region was specified. Boto3 requires a region to be set, either explicitly in the client call, via environment variables (`AWS_REGION` or `AWS_DEFAULT_REGION`), or in AWS configuration files (`~/.aws/config`).
Install
-
pip install mypy-boto3-secretsmanager boto3
Imports
- SecretsManagerClient
from mypy_boto3_secretsmanager import SecretsManagerClient
- GetSecretValueResponseTypeDef
from mypy_boto3_secretsmanager import GetSecretValueResponse
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.")