Skip to content

Commit bd18cc6

Browse files
authored
Propagate EmailMessage generics (#15639)
1 parent c03c2b9 commit bd18cc6

File tree

2 files changed

+15
-5
lines changed

2 files changed

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

55
msg = EmailMessage()
@@ -8,3 +8,11 @@
88

99
for a in msg.iter_attachments():
1010
assert_type(a, EmailMessage)
11+
12+
generic_msg: EmailMessage[BaseHeader, str] = EmailMessage()
13+
assert_type(generic_msg.get("To"), BaseHeader | None)
14+
assert_type(generic_msg.get_body(), MIMEPart[BaseHeader, str] | None)
15+
for a in generic_msg.iter_attachments():
16+
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: 5 additions & 3 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: ...
@@ -171,4 +173,4 @@ class MIMEPart(Message[_HeaderRegistryT_co, _HeaderRegistryParamT_contra]):
171173
def as_string(self, unixfrom: bool = False, maxheaderlen: int | None = None, policy: Policy[Any] | None = None) -> str: ...
172174
def is_attachment(self) -> bool: ...
173175

174-
class EmailMessage(MIMEPart): ...
176+
class EmailMessage(MIMEPart[_HeaderRegistryT_co, _HeaderRegistryParamT_contra]): ...

0 commit comments

Comments
 (0)