Skip to content

email.message_from_bytes etc return legacy Message objects, not EmailMessage #10762

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

Open
fsc-eriker opened this issue Sep 25, 2023 · 4 comments
Labels
stubs: false positive Type checkers report false errors

Comments

@fsc-eriker
Copy link

fsc-eriker commented Sep 25, 2023

When you run MyPy on code which requires an EmailMessage, you often get errors.

from email import message_from_binary_file
from email.message import EmailMessage
from email.policy import default

def fn(msg: EmailMessage) -> None:
    print(type(msg))

with open("/tmp/message.eml", "rb") as emlbytes:
    message = message_from_binary_file(emlbytes, policy=default)
fn(message)

Running mypy on this file gets me

bash$ mypy /tmp/mypy.py 
/tmp/mypy.py:10: error: Argument 1 to "fn" has incompatible type "Message"; expected "EmailMessage"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

You can obviously work around this with a cast or etc; but perhaps the default for these methods should be changed to return an EmailMessage, and require a cast if you need the legacy Message type instead?

I get similar behavior with email_from_bytes etc; all of these functions for producing an EmailMessage object from the RFC5322 representation of an actual message have an API which (obscurely!) depends on the presence of the policy= argument for whether to return an EmailMessage or a legacy Message.

@fsc-eriker

This comment was marked as resolved.

@fsc-eriker fsc-eriker changed the title mypy insists that email.from_bytes etc return legacy email.message.Message objects, not email.message.EmailMessage mypy insists that email.message_from_bytes etc return legacy email.message.Message objects, not email.message.EmailMessage Sep 25, 2023
@AlexWaygood AlexWaygood transferred this issue from python/cpython Sep 25, 2023
@srittau
Copy link
Collaborator

srittau commented Sep 25, 2023

This also applies to the return types of the different email.parser.Parser classes.

We should consider making the Parser classes and the functions generic, similar to FeedParser. For this to work, we would also need to make Policy generic over the message class. Any PRs welcome!

@srittau srittau added the stubs: false positive Type checkers report false errors label Sep 25, 2023
@AlexWaygood
Copy link
Member

(FYI, @fsc-eriker, I transferred this issue from CPython since CPython itself has no type hints; all type hints for the standard library are found here, in the typeshed repo :)

srittau added a commit to srittau/typeshed that referenced this issue Sep 25, 2023
@srittau
Copy link
Collaborator

srittau commented Sep 25, 2023

#10769 is a first step by making policies generic.

@srittau srittau changed the title mypy insists that email.message_from_bytes etc return legacy email.message.Message objects, not email.message.EmailMessage email.message_from_bytes etc return legacy Message objects, not EmailMessage Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: false positive Type checkers report false errors
Projects
None yet
Development

No branches or pull requests

3 participants