Skip to content

Commit

Permalink
Possible _HeaderType improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Dec 1, 2023
1 parent 17ff8a0 commit a60f6a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
4 changes: 3 additions & 1 deletion 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 @@ -31,7 +32,8 @@ class Message:
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

0 comments on commit a60f6a8

Please sign in to comment.