typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

mypy and pyright falsely report that objects returned by email.message.EmailMessage.iter_attachments aren't of type EmailMessage

Open butterthebuddha opened this issue 1 year ago • 0 comments

Hi folks,

For the simple program

from email.message import EmailMessage


def process_attachment(a: EmailMessage) -> None:
    assert isinstance(a, EmailMessage)


msg = EmailMessage()
msg.add_attachment(b"foo", maintype="application", subtype="pdf")
for a in msg.iter_attachments():
    process_attachment(a)

which I can run without issues with Python 3.9.6, I get the following errors reported by mypy 1.11.1 and pyright 1.1.376:

$ mypy test.py
test.py:11: error: Argument 1 to "process_attachment" has incompatible type "MIMEPart[Any, Any]"; expected "EmailMessage"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

$ pyright test.py
/Users/ani/Scratch/test.py
  /Users/ani/Scratch/test.py:11:24 - error: Argument of type "MIMEPart[Any, Any]" cannot be assigned to parameter "a" of type "EmailMessage" in function "process_attachment"
    "MIMEPart[Any, Any]" is incompatible with "EmailMessage" (reportArgumentType)
1 error, 0 warnings, 0 informations 

I know there are a few PRs floating around to make type checking for the email module more accurate, but I'm not sure if any of them apply to this situation. Please have a look, thanks!

butterthebuddha avatar Aug 15 '24 13:08 butterthebuddha