Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make various email.Policy use sites generic over the message type #13274

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
34 changes: 28 additions & 6 deletions stdlib/email/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections.abc import Callable
from email.message import Message
from email.policy import Policy
from typing import IO
from email.policy import Policy, _MessageT
from typing import IO, overload
from typing_extensions import TypeAlias

# At runtime, listing submodules in __all__ without them being imported is
Expand Down Expand Up @@ -31,7 +31,29 @@ __all__ = [ # noqa: F822 # Undefined names in __all__
_ParamType: TypeAlias = str | tuple[str | None, str | None, str] # noqa: Y047
_ParamsType: TypeAlias = str | None | tuple[str, str | None, str] # noqa: Y047

def message_from_string(s: str, _class: Callable[[], Message] = ..., *, policy: Policy = ...) -> Message: ...
def message_from_bytes(s: bytes | bytearray, _class: Callable[[], Message] = ..., *, policy: Policy = ...) -> Message: ...
def message_from_file(fp: IO[str], _class: Callable[[], Message] = ..., *, policy: Policy = ...) -> Message: ...
def message_from_binary_file(fp: IO[bytes], _class: Callable[[], Message] = ..., *, policy: Policy = ...) -> Message: ...
@overload
def message_from_string(s: str) -> Message: ...
@overload
def message_from_string(s: str, _class: Callable[[], _MessageT]) -> _MessageT: ...
@overload
def message_from_string(s: str, _class: Callable[[], _MessageT] = ..., *, policy: Policy[_MessageT]) -> _MessageT: ...
@overload
def message_from_bytes(s: bytes | bytearray) -> Message: ...
@overload
def message_from_bytes(s: bytes | bytearray, _class: Callable[[], _MessageT]) -> _MessageT: ...
@overload
def message_from_bytes(
s: bytes | bytearray, _class: Callable[[], _MessageT] = ..., *, policy: Policy[_MessageT]
) -> _MessageT: ...
@overload
def message_from_file(fp: IO[str]) -> Message: ...
@overload
def message_from_file(fp: IO[str], _class: Callable[[], _MessageT]) -> _MessageT: ...
@overload
def message_from_file(fp: IO[str], _class: Callable[[], _MessageT] = ..., *, policy: Policy[_MessageT]) -> _MessageT: ...
@overload
def message_from_binary_file(fp: IO[bytes]) -> Message: ...
@overload
def message_from_binary_file(fp: IO[bytes], _class: Callable[[], _MessageT]) -> _MessageT: ...
@overload
def message_from_binary_file(fp: IO[bytes], _class: Callable[[], _MessageT] = ..., *, policy: Policy[_MessageT]) -> _MessageT: ...
5 changes: 2 additions & 3 deletions stdlib/email/mime/message.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from email.message import Message
from email.mime.nonmultipart import MIMENonMultipart
from email.policy import Policy
from email.policy import Policy, _MessageT

__all__ = ["MIMEMessage"]

class MIMEMessage(MIMENonMultipart):
def __init__(self, _msg: Message, _subtype: str = "rfc822", *, policy: Policy | None = None) -> None: ...
def __init__(self, _msg: _MessageT, _subtype: str = "rfc822", *, policy: Policy[_MessageT] | None = None) -> None: ...
7 changes: 3 additions & 4 deletions stdlib/email/mime/multipart.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from collections.abc import Sequence
from email import _ParamsType
from email.message import Message
from email.mime.base import MIMEBase
from email.policy import Policy
from email.policy import Policy, _MessageT

__all__ = ["MIMEMultipart"]

Expand All @@ -11,8 +10,8 @@ class MIMEMultipart(MIMEBase):
self,
_subtype: str = "mixed",
boundary: str | None = None,
_subparts: Sequence[Message] | None = None,
_subparts: Sequence[_MessageT] | None = None,
*,
policy: Policy | None = None,
policy: Policy[_MessageT] | None = None,
**_params: _ParamsType,
) -> None: ...
Loading