Skip to content

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

Open
@fsc-eriker

Description

@fsc-eriker

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions