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

Reduce code duplication in the email module #7558

Merged
merged 4 commits into from
Apr 6, 2022
Merged
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
7 changes: 6 additions & 1 deletion stdlib/email/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from email.message import Message
from email.policy import Policy
from typing import IO, Callable
from typing import IO, Callable, TypeVar, Union

# Definitions imported by multiple submodules in typeshed
_MessageT = TypeVar("_MessageT", bound=Message) # noqa: Y018
_ParamType = Union[str, tuple[str | None, str | None, str]]
_ParamsType = Union[str, None, tuple[str, str | None, str]]

def message_from_string(s: str, _class: Callable[[], Message] = ..., *, policy: Policy = ...) -> Message: ...
def message_from_bytes(s: bytes, _class: Callable[[], Message] = ..., *, policy: Policy = ...) -> Message: ...
Expand Down
17 changes: 8 additions & 9 deletions stdlib/email/feedparser.pyi
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
from email import _MessageT
from email.message import Message
from email.policy import Policy
from typing import Callable, Generic, TypeVar, overload
from typing import Callable, Generic, overload

__all__ = ["FeedParser", "BytesFeedParser"]

_M = TypeVar("_M", bound=Message)

class FeedParser(Generic[_M]):
class FeedParser(Generic[_MessageT]):
@overload
def __init__(self: FeedParser[Message], _factory: None = ..., *, policy: Policy = ...) -> None: ...
@overload
def __init__(self, _factory: Callable[[], _M], *, policy: Policy = ...) -> None: ...
def __init__(self, _factory: Callable[[], _MessageT], *, policy: Policy = ...) -> None: ...
def feed(self, data: str) -> None: ...
def close(self) -> _M: ...
def close(self) -> _MessageT: ...

class BytesFeedParser(Generic[_M]):
class BytesFeedParser(Generic[_MessageT]):
@overload
def __init__(self: BytesFeedParser[Message], _factory: None = ..., *, policy: Policy = ...) -> None: ...
@overload
def __init__(self, _factory: Callable[[], _M], *, policy: Policy = ...) -> None: ...
def __init__(self, _factory: Callable[[], _MessageT], *, policy: Policy = ...) -> None: ...
def feed(self, data: bytes) -> None: ...
def close(self) -> _M: ...
def close(self) -> _MessageT: ...
5 changes: 2 additions & 3 deletions stdlib/email/message.pyi
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
from email import _ParamsType, _ParamType
from email.charset import Charset
from email.contentmanager import ContentManager
from email.errors import MessageDefect
from email.policy import Policy
from typing import Any, Generator, Iterator, Sequence, TypeVar, Union
from typing import Any, Generator, Iterator, Sequence, TypeVar

__all__ = ["Message", "EmailMessage"]

_T = TypeVar("_T")

_PayloadType = list[Message] | str | bytes
_CharsetType = Charset | str | None
_ParamsType = Union[str, None, tuple[str, str | None, str]]
_ParamType = Union[str, tuple[str | None, str | None, str]]
_HeaderType = Any

class Message:
Expand Down
5 changes: 2 additions & 3 deletions stdlib/email/mime/application.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from collections.abc import Callable
from email import _ParamsType
from email.mime.nonmultipart import MIMENonMultipart
from email.policy import Policy
from typing import Callable, Union

__all__ = ["MIMEApplication"]

_ParamsType = Union[str, None, tuple[str, str | None, str]]

class MIMEApplication(MIMENonMultipart):
def __init__(
self,
Expand Down
5 changes: 2 additions & 3 deletions stdlib/email/mime/audio.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from collections.abc import Callable
from email import _ParamsType
from email.mime.nonmultipart import MIMENonMultipart
from email.policy import Policy
from typing import Callable, Union

__all__ = ["MIMEAudio"]

_ParamsType = Union[str, None, tuple[str, str | None, str]]

class MIMEAudio(MIMENonMultipart):
def __init__(
self,
Expand Down
4 changes: 1 addition & 3 deletions stdlib/email/mime/base.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import email.message
from email import _ParamsType
from email.policy import Policy
from typing import Union

__all__ = ["MIMEBase"]

_ParamsType = Union[str, None, tuple[str, str | None, str]]

class MIMEBase(email.message.Message):
def __init__(self, _maintype: str, _subtype: str, *, policy: Policy | None = ..., **_params: _ParamsType) -> None: ...
5 changes: 2 additions & 3 deletions stdlib/email/mime/image.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from collections.abc import Callable
from email import _ParamsType
from email.mime.nonmultipart import MIMENonMultipart
from email.policy import Policy
from typing import Callable, Union

__all__ = ["MIMEImage"]

_ParamsType = Union[str, None, tuple[str, str | None, str]]

class MIMEImage(MIMENonMultipart):
def __init__(
self,
Expand Down
5 changes: 2 additions & 3 deletions stdlib/email/mime/multipart.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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 typing import Sequence, Union

__all__ = ["MIMEMultipart"]

_ParamsType = Union[str, None, tuple[str, str | None, str]]

class MIMEMultipart(MIMEBase):
def __init__(
self,
Expand Down
9 changes: 4 additions & 5 deletions stdlib/email/parser.pyi
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import email.feedparser
from email import _MessageT
from email.message import Message
from email.policy import Policy
from typing import BinaryIO, Callable, TextIO, TypeVar
from typing import BinaryIO, Callable, TextIO

__all__ = ["Parser", "HeaderParser", "BytesParser", "BytesHeaderParser", "FeedParser", "BytesFeedParser"]

_M = TypeVar("_M", bound=Message)

FeedParser = email.feedparser.FeedParser[_M]
BytesFeedParser = email.feedparser.BytesFeedParser[_M]
FeedParser = email.feedparser.FeedParser[_MessageT]
BytesFeedParser = email.feedparser.BytesFeedParser[_MessageT]

class Parser:
def __init__(self, _class: Callable[[], Message] | None = ..., *, policy: Policy = ...) -> None: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/email/utils.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import datetime
import sys
from email import _ParamType
from email.charset import Charset
from typing import Union, overload
from typing import overload

__all__ = [
"collapse_rfc2231_value",
Expand All @@ -21,7 +22,6 @@ __all__ = [
"unquote",
]

_ParamType = Union[str, tuple[str | None, str | None, str]]
_PDTZ = tuple[int, int, int, int, int, int, int, int, int, int | None]

def quote(str: str) -> str: ...
Expand Down