Skip to content

Commit 85500e3

Browse files
committed
Preserve MIMEPart header param in returns
Commit 56db30e made Message generic and switched get_body() and iter_parts() to return MIMEPart[_HeaderRegistryT]. That dropped the second type parameter, so the header parameter type defaulted to Any. Preserve both type parameters in the return types and add a regression test covering get_body() and iter_parts().
1 parent 9356f50 commit 85500e3

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

stdlib/@tests/test_cases/email/check_message.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from email.headerregistry import Address, BaseHeader
2-
from email.message import EmailMessage
2+
from email.message import EmailMessage, MIMEPart
33
from typing_extensions import assert_type
44

55
msg = EmailMessage()
@@ -11,5 +11,8 @@
1111

1212
generic_msg: EmailMessage[BaseHeader, str] = EmailMessage()
1313
assert_type(generic_msg.get("To"), BaseHeader | None)
14+
assert_type(generic_msg.get_body(), MIMEPart[BaseHeader, str] | None)
1415
for a in generic_msg.iter_attachments():
1516
assert_type(a, EmailMessage[BaseHeader, str])
17+
for p in generic_msg.iter_parts():
18+
assert_type(p, MIMEPart[BaseHeader, str])

stdlib/email/message.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,15 @@ class Message(Generic[_HeaderT_co, _HeaderParamT_contra]):
151151

152152
class MIMEPart(Message[_HeaderRegistryT_co, _HeaderRegistryParamT_contra]):
153153
def __init__(self, policy: Policy[Any] | None = None) -> None: ...
154-
def get_body(self, preferencelist: Sequence[str] = ("related", "html", "plain")) -> MIMEPart[_HeaderRegistryT_co] | None: ...
154+
def get_body(
155+
self, preferencelist: Sequence[str] = ("related", "html", "plain")
156+
) -> MIMEPart[_HeaderRegistryT_co, _HeaderRegistryParamT_contra] | None: ...
155157
def attach(self, payload: Self) -> None: ... # type: ignore[override]
156158
# The attachments are created via type(self) in the attach method. It's theoretically
157159
# possible to sneak other attachment types into a MIMEPart instance, but could cause
158160
# cause unforseen consequences.
159161
def iter_attachments(self) -> Iterator[Self]: ...
160-
def iter_parts(self) -> Iterator[MIMEPart[_HeaderRegistryT_co]]: ...
162+
def iter_parts(self) -> Iterator[MIMEPart[_HeaderRegistryT_co, _HeaderRegistryParamT_contra]]: ...
161163
def get_content(self, *args: Any, content_manager: ContentManager | None = None, **kw: Any) -> Any: ...
162164
def set_content(self, *args: Any, content_manager: ContentManager | None = None, **kw: Any) -> None: ...
163165
def make_related(self, boundary: str | None = None) -> None: ...

0 commit comments

Comments
 (0)