Mypy Boto3 SNS Type Stubs
mypy-boto3-sns provides type annotations for the AWS Boto3 SNS (Simple Notification Service) client and resources. It enhances development experience with type checking tools like mypy, pyright, and IDEs (VSCode, PyCharm) by offering static analysis for Boto3 code. This package, currently at version 1.42.3, is generated by `mypy-boto3-builder` (version 8.12.0) and typically releases in sync with `boto3` updates.
Warnings
- breaking Removed support for Python 3.8. The `mypy-boto3-builder` 8.12.0, which generated `mypy-boto3-sns` 1.42.3, no longer supports Python 3.8. Users on Python 3.8 must either upgrade their Python version or use an older version of `mypy-boto3-sns`.
- breaking Breaking changes to `TypeDef` naming conventions. In `mypy-boto3-builder` 8.9.0 (and potentially affecting later versions), `TypeDef` names for packed method arguments were shortened (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`). Additionally, conflicting `Extra` postfixes were moved to the end of the name (e.g., `CreateDistributionExtraRequestTypeDef` became `CreateDistributionRequestExtraTypeDef`). While not specific to SNS in the release notes, this applies to `[services]` generated by the builder and may affect users relying on specific `TypeDef` names.
- gotcha The library only provides type hints; it does not add runtime functionality to `boto3`. `mypy-boto3-sns` is a stub package that helps static type checkers (like mypy) understand `boto3`'s API. It is not a replacement for `boto3` and has no runtime impact on your application.
- gotcha `pylint` may complain about undefined variables when using `typing.TYPE_CHECKING` for conditional imports. This is a known issue.
- gotcha There are two main ways to install boto3 stubs: `mypy-boto3-sns` (standalone) or `boto3-stubs[sns]` (part of the `boto3-stubs` meta-package). Confusing these or mixing installations can lead to unexpected type-checking behavior.
Install
-
pip install mypy-boto3-sns boto3 mypy
Imports
- SNSClient
from mypy_boto3_sns.client import SNSClient
- SNSServiceResource
from mypy_boto3_sns.service_resource import SNSServiceResource
- AddPermissionInputTopicAddPermissionTypeDef
from mypy_boto3_sns.type_defs import AddPermissionInputTopicAddPermissionTypeDef
Quickstart
import boto3
from typing import TYPE_CHECKING
from mypy_boto3_sns.client import SNSClient
# Boto3 client without explicit type annotation (type checker will infer)
def get_sns_client_inferred():
return boto3.client("sns")
# Boto3 client with explicit type annotation (recommended for type checking)
def get_sns_client_typed() -> SNSClient:
return boto3.client("sns")
if TYPE_CHECKING:
# Example usage with type checking
sns_client: SNSClient = get_sns_client_typed()
response = sns_client.publish(TopicArn="arn:aws:sns:REGION:ACCOUNT:TOPIC", Message="Hello, SNS!")
print(f"Message ID: {response['MessageId']}")
# Normal runtime usage (type hints are ignored by the interpreter)
sns_client_runtime = get_sns_client_inferred()
# You would typically have actual topic/message data here,
# but for a quickstart, we keep it simple.
# Replace with a valid TopicArn for actual execution.
# response_runtime = sns_client_runtime.publish(TopicArn="arn:aws:sns:REGION:ACCOUNT:TOPIC", Message="Hello from runtime!")
# print(f"Runtime Message ID: {response_runtime['MessageId']}")