Type Annotations for Boto3 Pinpoint Email
mypy-boto3-pinpoint-email provides comprehensive type annotations for the Amazon Pinpoint Email Service client in the boto3 library, ensuring static type checking with tools like Mypy, improved IDE auto-completion, and early detection of potential runtime errors. It is currently at version 1.42.3 and is actively maintained with updates released in sync with boto3 and botocore, generated by the mypy-boto3-builder.
Warnings
- breaking As of mypy-boto3-builder 8.12.0, Python 3.8 support has been removed. Ensure your projects use Python 3.9 or newer.
- breaking Type definition (TypeDef) naming conventions underwent breaking changes in mypy-boto3-builder 8.9.0. This may affect direct imports of TypeDefs from `mypy_boto3_pinpoint_email.type_defs` or similar modules, potentially leading to 'name not found' errors.
- gotcha For optimal IDE auto-completion and type hinting, especially in VSCode or PyCharm, explicit type annotations are recommended when obtaining a client, e.g., `client: PinpointEmailClient = session.client('pinpoint-email')`.
- gotcha Pylint may report undefined variable errors when using `typing.TYPE_CHECKING` guards. A workaround is to conditionally assign `object` to types when `TYPE_CHECKING` is false.
- deprecated While not directly for `pinpoint-email`, a related service, `sms-voice`, was deprecated in mypy-boto3-builder 8.11.0 in favor of `pinpoint-sms-voice`. This highlights a general pattern where AWS service name changes can lead to stub package changes, requiring updates to imports for affected services.
Install
-
pip install boto3 mypy-boto3-pinpoint-email
Imports
- PinpointEmailClient
from mypy_boto3_pinpoint_email.client import PinpointEmailClient
- Session
from boto3.session import Session
Quickstart
import os
from typing import TYPE_CHECKING
import boto3
from boto3.session import Session
# Import PinpointEmailClient for type hinting, conditionally for runtime performance
if TYPE_CHECKING:
from mypy_boto3_pinpoint_email.client import PinpointEmailClient
from mypy_boto3_pinpoint_email.type_defs import SendEmailResponseTypeDef
def send_pinpoint_email(recipient_email: str, sender_email: str, subject: str, body: str) -> None:
"""Sends an email using AWS Pinpoint Email service."""
session = Session()
client: 'PinpointEmailClient' = session.client("pinpoint-email")
response: 'SendEmailResponseTypeDef' = client.send_email(
FromEmailAddress=sender_email,
Destination={
'ToAddresses': [
recipient_email,
],
},
Content={
'Simple': {
'Subject': {
'Data': subject,
'Charset': 'UTF-8'
},
'Body': {
'Text': {
'Data': body,
'Charset': 'UTF-8'
}
}
}
}
)
print(f"Email sent! Message ID: {response['MessageId']}")
if __name__ == "__main__":
# Replace with your verified sender and recipient email addresses
# Ensure these are set as environment variables for production
SENDER = os.environ.get('PINPOINT_SENDER_EMAIL', 'verified@example.com')
RECIPIENT = os.environ.get('PINPOINT_RECIPIENT_EMAIL', 'recipient@example.com')
EMAIL_SUBJECT = "Hello from mypy-boto3 Pinpoint Email!"
EMAIL_BODY = "This is a test email sent using AWS Pinpoint Email with type annotations."
if SENDER == 'verified@example.com' or RECIPIENT == 'recipient@example.com':
print("Please set PINPOINT_SENDER_EMAIL and PINPOINT_RECIPIENT_EMAIL environment variables.")
print("Make sure the sender email is verified in AWS Pinpoint Email.")
else:
send_pinpoint_email(RECIPIENT, SENDER, EMAIL_SUBJECT, EMAIL_BODY)