Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
send_email: Parse emails in a way mypy 1.14 understands.
Anders Kaseorg63aaafb94a3f
diff --git a/zerver/lib/send_email.py b/zerver/lib/send_email.pyindex 348e1925651..bc12df5c4e9 100644--- a/zerver/lib/send_email.py+++ b/zerver/lib/send_email.py@@ -6,6 +6,7 @@ from collections.abc import Callable, Mappingfrom contextlib import suppressfrom datetime import timedeltafrom email.headerregistry import Address+from email.message import EmailMessagefrom email.parser import Parserfrom email.policy import defaultfrom email.utils import formataddr, parseaddr@@ -526,7 +527,7 @@ def custom_email_sender() -> Callable[..., None]:with open(markdown_template_path) as f:text = f.read()- parsed_email_template = Parser(policy=default).parsestr(text)+ parsed_email_template = Parser(_class=EmailMessage, policy=default).parsestr(text)email_template_hash = hashlib.sha256(text.encode()).hexdigest()[0:32]email_id = f"zerver/emails/custom/custom_email_{email_template_hash}"diff --git a/zerver/management/commands/email_mirror.py b/zerver/management/commands/email_mirror.pyindex 6f5faa298bb..2de82691b14 100644--- a/zerver/management/commands/email_mirror.py+++ b/zerver/management/commands/email_mirror.py@@ -18,7 +18,7 @@ recipient address and retrieve, forward, and archive the message."""-import email+import email.parserimport email.policyimport loggingfrom collections.abc import Generator@@ -69,10 +69,9 @@ def get_imap_messages() -> Generator[EmailMessage, None, None]:status, msg_data = mbox.fetch(message_id, "(RFC822)")assert isinstance(msg_data[0], tuple)msg_as_bytes = msg_data[0][1]- message = email.message_from_bytes(msg_as_bytes, policy=email.policy.default)- # https://github.com/python/typeshed/issues/2417- assert isinstance(message, EmailMessage)- yield message+ yield email.parser.BytesParser(+ _class=EmailMessage, policy=email.policy.default+ ).parsebytes(msg_as_bytes)mbox.store(message_id, "+FLAGS", "\\Deleted")mbox.expunge()finally:diff --git a/zerver/management/commands/send_to_email_mirror.py b/zerver/management/commands/send_to_email_mirror.pyindex 9ab132871cd..8cfebfc4c0e 100644--- a/zerver/management/commands/send_to_email_mirror.py+++ b/zerver/management/commands/send_to_email_mirror.py@@ -1,5 +1,5 @@import base64-import email+import email.parserimport email.policyimport osfrom email.message import EmailMessage@@ -105,10 +105,9 @@ Example:return self._parse_email_json_fixture(fixture_path)else:with open(fixture_path, "rb") as fp:- message = email.message_from_binary_file(fp, policy=email.policy.default)- # https://github.com/python/typeshed/issues/2417- assert isinstance(message, EmailMessage)- return message+ return email.parser.BytesParser(+ _class=EmailMessage, policy=email.policy.default+ ).parse(fp)def _prepare_message(self, message: EmailMessage, realm: Realm, stream_name: str) -> None:stream = get_stream(stream_name, realm)diff --git a/zerver/tests/test_email_mirror.py b/zerver/tests/test_email_mirror.pyindex b76fc69bcd2..ada0047fc2c 100644--- a/zerver/tests/test_email_mirror.py+++ b/zerver/tests/test_email_mirror.py@@ -1,9 +1,9 @@import base64+import email.parserimport email.policyimport osimport subprocessfrom collections.abc import Callable, Mapping-from email import message_from_stringfrom email.headerregistry import Addressfrom email.message import EmailMessage, MIMEPartfrom typing import TYPE_CHECKING, Any@@ -1671,9 +1671,9 @@ class TestContentTypeUnspecifiedCharset(ZulipTestCase):message_as_string = message_as_string.replace('Content-Type: text/plain; charset="us-ascii"', "Content-Type: text/plain")- incoming_message = message_from_string(message_as_string, policy=email.policy.default)- # https://github.com/python/typeshed/issues/2417- assert isinstance(incoming_message, EmailMessage)+ incoming_message = email.parser.Parser(+ _class=EmailMessage, policy=email.policy.default+ ).parsestr(message_as_string)user_profile = self.example_user("hamlet")self.login_user(user_profile)@@ -1696,9 +1696,9 @@ class TestContentTypeInvalidCharset(ZulipTestCase):'Content-Type: text/plain; charset="us-ascii"','Content-Type: text/plain; charset="bogus"',)- incoming_message = message_from_string(message_as_string, policy=email.policy.default)- # https://github.com/python/typeshed/issues/2417- assert isinstance(incoming_message, EmailMessage)+ incoming_message = email.parser.Parser(+ _class=EmailMessage, policy=email.policy.default+ ).parsestr(message_as_string)user_profile = self.example_user("hamlet")self.login_user(user_profile)diff --git a/zerver/worker/email_mirror.py b/zerver/worker/email_mirror.pyindex 755241b4175..dabe82fc692 100644--- a/zerver/worker/email_mirror.py+++ b/zerver/worker/email_mirror.py@@ -1,6 +1,6 @@# Documented in https://zulip.readthedocs.io/en/latest/subsystems/queuing.htmlimport base64-import email+import email.parserimport email.policyimport loggingfrom collections.abc import Mapping@@ -29,11 +29,9 @@ class MirrorWorker(QueueProcessingWorker):def consume(self, event: Mapping[str, Any]) -> None:rcpt_to = event["rcpt_to"]content = base64.b64decode(event["msg_base64"])- msg = email.message_from_bytes(- content,- policy=email.policy.default,+ msg = email.parser.BytesParser(_class=EmailMessage, policy=email.policy.default).parsebytes(+ content)- assert isinstance(msg, EmailMessage) # https://github.com/python/typeshed/issues/2417if not is_missed_message_address(rcpt_to):# Missed message addresses are one-time use, so we don't need# to worry about emails to them resulting in message spam.