Use Self for email.message attachments (#12530)

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Avasam <samuel.06@hotmail.com>
This commit is contained in:
Sebastian Rittau
2024-08-29 16:48:32 +02:00
committed by GitHub
parent 10a083cfb1
commit 039c6bcdb1
2 changed files with 9 additions and 1 deletions

View File

@@ -1,6 +1,10 @@
from email.headerregistry import Address
from email.message import EmailMessage
from typing_extensions import assert_type
msg = EmailMessage()
msg["To"] = "receiver@example.com"
msg["From"] = Address("Sender Name", "sender", "example.com")
for a in msg.iter_attachments():
assert_type(a, EmailMessage)

View File

@@ -147,7 +147,11 @@ class Message(Generic[_HeaderT, _HeaderParamT]):
class MIMEPart(Message[_HeaderRegistryT, _HeaderRegistryParamT]):
def __init__(self, policy: Policy | None = None) -> None: ...
def get_body(self, preferencelist: Sequence[str] = ("related", "html", "plain")) -> MIMEPart[_HeaderRegistryT] | None: ...
def iter_attachments(self) -> Iterator[MIMEPart[_HeaderRegistryT]]: ...
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]]: ...
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: ...