From a31f6312d97ebd4a0e1b5622c621b5c113f5c17d Mon Sep 17 00:00:00 2001 From: Ian Campbell Date: Fri, 23 Aug 2024 12:06:10 -0400 Subject: [PATCH] Fix issues related to type hinting. --- anymail/backends/sendgrid.py | 3 ++- anymail/backends/unisender_go.py | 5 +++-- anymail/exceptions.py | 2 +- anymail/webhooks/base.py | 4 +++- tests/test_sparkpost_backend.py | 2 +- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/anymail/backends/sendgrid.py b/anymail/backends/sendgrid.py index b19e884d..475c010a 100644 --- a/anymail/backends/sendgrid.py +++ b/anymail/backends/sendgrid.py @@ -1,12 +1,13 @@ import uuid import warnings +from collections.abc import Mapping from email.utils import quote as rfc822_quote from requests.structures import CaseInsensitiveDict from ..exceptions import AnymailConfigurationError, AnymailWarning from ..message import AnymailRecipientStatus -from ..utils import BASIC_NUMERIC_TYPES, Mapping, get_anymail_setting, update_deep +from ..utils import BASIC_NUMERIC_TYPES, get_anymail_setting, update_deep from .base_requests import AnymailRequestsBackend, RequestsPayload diff --git a/anymail/backends/unisender_go.py b/anymail/backends/unisender_go.py index c70a3a33..a1e187d6 100644 --- a/anymail/backends/unisender_go.py +++ b/anymail/backends/unisender_go.py @@ -135,8 +135,9 @@ def __init__( http_headers["Content-Type"] = "application/json" http_headers["Accept"] = "application/json" http_headers["X-API-KEY"] = backend.api_key + kwargs["headers"] = http_headers super().__init__( - message, defaults, backend, headers=http_headers, *args, **kwargs + message, defaults, backend, *args, **kwargs ) def get_api_endpoint(self) -> str: @@ -297,7 +298,7 @@ def set_send_at(self, send_at: datetime | str) -> None: # "Date and time in the format “YYYY-MM-DD hh:mm:ss” in the UTC time zone." # If send_at is a datetime, it's guaranteed to be aware, but maybe not UTC. # Convert to UTC, then strip tzinfo to avoid isoformat "+00:00" at end. - send_at_utc = send_at.astimezone(timezone.utc).replace(tzinfo=None) + send_at_utc = send_at.astimezone(timezone.utc).replace(tzinfo=None) # type:ignore[union-attr] send_at_formatted = send_at_utc.isoformat(sep=" ", timespec="seconds") assert len(send_at_formatted) == 19 except (AttributeError, TypeError): diff --git a/anymail/exceptions.py b/anymail/exceptions.py index 98dc9e07..4fe548c0 100644 --- a/anymail/exceptions.py +++ b/anymail/exceptions.py @@ -86,7 +86,7 @@ class AnymailAPIError(AnymailError): """Exception for unsuccessful response from ESP's API.""" -class AnymailRequestsAPIError(AnymailAPIError, HTTPError): +class AnymailRequestsAPIError(AnymailAPIError, HTTPError): # type: ignore[misc] """Exception for unsuccessful response from a requests API.""" def __init__(self, *args, **kwargs): diff --git a/anymail/webhooks/base.py b/anymail/webhooks/base.py index 5a2aefe8..8622c1a8 100644 --- a/anymail/webhooks/base.py +++ b/anymail/webhooks/base.py @@ -1,5 +1,7 @@ +import typing import warnings +from django.dispatch import Signal from django.http import HttpResponse from django.utils.crypto import constant_time_compare from django.utils.decorators import method_decorator @@ -30,7 +32,7 @@ def __init__(self, **kwargs): # Subclass implementation: # Where to send events: either ..signals.inbound or ..signals.tracking - signal = None + signal: typing.Optional[Signal] = None def validate_request(self, request): """Check validity of webhook post, or raise AnymailWebhookValidationFailure. diff --git a/tests/test_sparkpost_backend.py b/tests/test_sparkpost_backend.py index bfb44024..906539b5 100644 --- a/tests/test_sparkpost_backend.py +++ b/tests/test_sparkpost_backend.py @@ -49,7 +49,7 @@ class SparkPostBackendMockAPITestCase(RequestsBackendMockAPITestCase): def setUp(self): super().setUp() # Simple message useful for many tests - self.message = mail.EmailMultiAlternatives( + self.message = AnymailMessage( "Subject", "Text Body", "from@example.com", ["to@example.com"] )