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

Exploring generic Policy.header_fetch_parse for python#2333 #36

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 4 additions & 2 deletions stdlib/email/message.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ _T = TypeVar("_T")

_PayloadType: TypeAlias = list[Message] | str | bytes | bytearray
_CharsetType: TypeAlias = Charset | str | None
# Type returned by Policy.header_fetch_parse, AnyOf[str | Header]
_HeaderType: TypeAlias = Any

class Message:
Expand All @@ -24,14 +25,15 @@ class Message:
def set_unixfrom(self, unixfrom: str) -> None: ...
def get_unixfrom(self) -> str | None: ...
def attach(self, payload: Message) -> None: ...
def get_payload(self, i: int | None = None, decode: bool = False) -> Any: ... # returns _PayloadType | None
def get_payload(self, i: int | None = None, decode: bool = False) -> Any: ... # AnyOf[_PayloadType, None]
def set_payload(self, payload: _PayloadType, charset: _CharsetType = None) -> None: ...
def set_charset(self, charset: _CharsetType) -> None: ...
def get_charset(self) -> _CharsetType: ...
def __len__(self) -> int: ...
def __contains__(self, name: str) -> bool: ...
def __iter__(self) -> Iterator[str]: ...
def __getitem__(self, name: str) -> _HeaderType: ...
# Same as get with failobj=None
def __getitem__(self, name: str) -> _HeaderType | None: ...
def __setitem__(self, name: str, val: _HeaderType) -> None: ...
def __delitem__(self, name: str) -> None: ...
def keys(self) -> list[str]: ...
Expand Down
23 changes: 19 additions & 4 deletions stdlib/email/policy.pyi
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
from _typeshed import Unused
from abc import ABCMeta, abstractmethod
from collections.abc import Callable
from email.contentmanager import ContentManager
from email.errors import MessageDefect
from email.header import Header
from email.headerregistry import BaseHeader
from email.message import Message
from typing import Any
from typing import Any, Protocol, TypeVar, overload
from typing_extensions import Self

__all__ = ["Compat32", "compat32", "Policy", "EmailPolicy", "default", "strict", "SMTP", "HTTP"]

class _HasName(Protocol):
name: str

_HasNameT = TypeVar("_HasNameT", bound=_HasName)
_HeaderT = TypeVar("_HeaderT", bound=Header)
_StrBaseHeader = TypeVar("_StrBaseHeader", bound=str) # BaseHeader matches this bound

class Policy(metaclass=ABCMeta):
max_line_length: int | None
linesep: str
Expand All @@ -35,7 +44,7 @@ class Policy(metaclass=ABCMeta):
@abstractmethod
def header_store_parse(self, name: str, value: str) -> tuple[str, str]: ...
@abstractmethod
def header_fetch_parse(self, name: str, value: str) -> str: ...
def header_fetch_parse(self, name: str, value: str) -> str | Header: ...
@abstractmethod
def fold(self, name: str, value: str) -> str: ...
@abstractmethod
Expand All @@ -44,7 +53,10 @@ class Policy(metaclass=ABCMeta):
class Compat32(Policy):
def header_source_parse(self, sourcelines: list[str]) -> tuple[str, str]: ...
def header_store_parse(self, name: str, value: str) -> tuple[str, str]: ...
def header_fetch_parse(self, name: str, value: str) -> str | Header: ... # type: ignore[override]
@overload
def header_fetch_parse(self, name: Unused, value: _HeaderT) -> _HeaderT: ...
@overload
def header_fetch_parse(self, name: str, value: _StrBaseHeader) -> _StrBaseHeader | Header: ...
def fold(self, name: str, value: str) -> str: ...
def fold_binary(self, name: str, value: str) -> bytes: ...

Expand All @@ -71,7 +83,10 @@ class EmailPolicy(Policy):
) -> None: ...
def header_source_parse(self, sourcelines: list[str]) -> tuple[str, str]: ...
def header_store_parse(self, name: str, value: Any) -> tuple[str, Any]: ...
def header_fetch_parse(self, name: str, value: str) -> Any: ...
@overload
def header_fetch_parse(self, name: Unused, value: _HasNameT) -> _HasNameT: ...
@overload
def header_fetch_parse(self, name: str, value: str) -> BaseHeader: ...
def fold(self, name: str, value: str) -> Any: ...
def fold_binary(self, name: str, value: str) -> bytes: ...

Expand Down