Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions stdlib/@tests/test_cases/email/check_message.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from email.headerregistry import Address
from email.message import EmailMessage
from email.headerregistry import Address, BaseHeader
from email.message import EmailMessage, MIMEPart
from typing_extensions import assert_type

msg = EmailMessage()
Expand All @@ -8,3 +8,11 @@

for a in msg.iter_attachments():
assert_type(a, EmailMessage)

generic_msg: EmailMessage[BaseHeader, str] = EmailMessage()
assert_type(generic_msg.get("To"), BaseHeader | None)
assert_type(generic_msg.get_body(), MIMEPart[BaseHeader, str] | None)
for a in generic_msg.iter_attachments():
assert_type(a, EmailMessage[BaseHeader, str])
for p in generic_msg.iter_parts():
assert_type(p, MIMEPart[BaseHeader, str])
8 changes: 5 additions & 3 deletions stdlib/email/message.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,15 @@ class Message(Generic[_HeaderT_co, _HeaderParamT_contra]):

class MIMEPart(Message[_HeaderRegistryT_co, _HeaderRegistryParamT_contra]):
def __init__(self, policy: Policy[Any] | None = None) -> None: ...
def get_body(self, preferencelist: Sequence[str] = ("related", "html", "plain")) -> MIMEPart[_HeaderRegistryT_co] | None: ...
def get_body(
self, preferencelist: Sequence[str] = ("related", "html", "plain")
) -> MIMEPart[_HeaderRegistryT_co, _HeaderRegistryParamT_contra] | None: ...
def attach(self, payload: Self) -> None: ... # type: ignore[override]
# The attachments are created via type(self) in the attach method. It's theoretically
# possible to sneak other attachment types into a MIMEPart instance, but could cause
# cause unforseen consequences.
def iter_attachments(self) -> Iterator[Self]: ...
def iter_parts(self) -> Iterator[MIMEPart[_HeaderRegistryT_co]]: ...
def iter_parts(self) -> Iterator[MIMEPart[_HeaderRegistryT_co, _HeaderRegistryParamT_contra]]: ...
def get_content(self, *args: Any, content_manager: ContentManager | None = None, **kw: Any) -> Any: ...
def set_content(self, *args: Any, content_manager: ContentManager | None = None, **kw: Any) -> None: ...
def make_related(self, boundary: str | None = None) -> None: ...
Expand All @@ -171,4 +173,4 @@ class MIMEPart(Message[_HeaderRegistryT_co, _HeaderRegistryParamT_contra]):
def as_string(self, unixfrom: bool = False, maxheaderlen: int | None = None, policy: Policy[Any] | None = None) -> str: ...
def is_attachment(self) -> bool: ...

class EmailMessage(MIMEPart): ...
class EmailMessage(MIMEPart[_HeaderRegistryT_co, _HeaderRegistryParamT_contra]): ...