Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make various email.Policy use sites generic over the message type #13274

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

brianschubert
Copy link
Contributor

@brianschubert brianschubert commented Dec 21, 2024

Fixes #13273

email.Policy was made generic in #12724. A bunch of places where its used don't provide an explicit type argument and use the default argument of Message. However, since email.Policy is invariant in the message type, this causes problems when trying to pass Policy instances that use subtypes of Message.

This PR makes various places where email.Policy is used generic in the message type, allowing the message type to be inferred instead of always defaulting to Message.

For context:

import email.policy

>>> email.message_from_string("")
<email.message.Message object at 0x7d3bf00c2410>

>>> email.message_from_string("", policy=email.policy.default)
<email.message.EmailMessage object at 0x7d3bf00c24d0>  # <-- message type determined by policy

>>> email.message_from_string("", email.message.EmailMessage)
<email.message.EmailMessage object at 0x7d3bf00c25c0>  # <-- message type determined by _class

This comment has been minimized.

This comment has been minimized.

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@brianschubert
Copy link
Contributor Author

The overloads in email/__init__.pyi are needed to satisfy pyright. Mypy supports using the type variable default when no arguments are provided, but pyright does not. PEP 696 gives type checkers the freedom to either support this or not, so neither behavior is incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typeshed in mypy 1.14.0 prohibits using email.policy.default with email.message_from_string()
1 participant