Skip to content

Conversation

@alexei
Copy link
Contributor

@alexei alexei commented Nov 30, 2025

According to the Django docs on sending email the outbox attribute is:

a list with an EmailMessage instance for each message that would be sent.

When you send single-message emails, they are direct instances of EmailMessage. When the emails are multipart, they are instances of EmailMultiAlternatives. While EmailMultiAlternatives inherits from EmailMessage, declaring outbox as list[EmailMessage is only partially correct, because the EmailMultiAlternatives introduces some properties and methods of its own, such as alternatives. If you access any of them, the type checker will complain. I think, thus, the correct type is:

outbox: list[EmailMessage | EmailMultiAlternatives]

@alexei
Copy link
Contributor Author

alexei commented Nov 30, 2025

On another note, I'm not sure EmailMultiAlternatives.alternatives is correctly typed as list[tuple[bytes | EmailMessage | Message | SafeMIMEText | str, str]], when Django seems to create a list of EmailAlternatives:

https://github.com/django/django/blob/85586052e8d1d9f160b9f1b351e61a787a8e4bed/django/core/mail/message.py#L479-L481

) -> None: ...

outbox: list[EmailMessage]
outbox: list[EmailMessage | EmailMultiAlternatives]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Sequence?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Co-authored-by: sobolevn <mail@sobolevn.me>
@github-actions

This comment has been minimized.

@sobolevn
Copy link
Member

sobolevn commented Dec 2, 2025

Sorry, my suggestion was not correct. Let's revert this

@alexei
Copy link
Contributor Author

alexei commented Dec 2, 2025

@sobolevn see 844362c -- I switched to MutableSequence because we need to support e.g. append:

https://github.com/django/django/blob/dba622ebc1f6a6700b75303a65f8a334bd46bd8e/django/core/mail/backends/locmem.py#L31

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sobolevn sobolevn merged commit f0147ce into typeddjango:master Dec 3, 2025
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants