8000 email: improve bytes handling by JelleZijlstra · Pull Request #9032 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

email: improve bytes handling #9032

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

Merged
merged 3 commits into from
Nov 1, 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
2 changes: 1 addition & 1 deletion stdlib/email/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _ParamType: TypeAlias = Union[str, tuple[str | None, str | None, str]] # noqa:
_ParamsType: TypeAlias = Union[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, _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: ...

Expand Down
12 changes: 8 additions & 4 deletions stdlib/email/base64mime.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
__all__ = ["body_decode", "body_encode", "decode", "decodestring", "header_encode", "header_length"]

def header_length(bytearray: str | bytes) -> int: ...
def header_encode(header_bytes: str | bytes, charset: str = ...) -> str: ...
def body_encode(s: bytes, maxlinelen: int = ..., eol: str = ...) -> str: ...
def decode(string: str | bytes) -> bytes: ...
from _typeshed import ReadableBuffer

def header_length(bytearray: str | bytes | bytearray) -> int: ...
def header_encode(header_bytes: str | ReadableBuffer, charset: str = ...) -> str: ...

# First argument should be a buffer that supports slicing and len().
def body_encode(s: bytes | bytearray, maxlinelen: int = ..., eol: str = ...) -> str: ...
def decode(string: str | ReadableBuffer) -> bytes: ...

body_decode = decode
decodestring = decode
2 changes: 1 addition & 1 deletion stdlib/email/feedparser.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ class BytesFeedParser(Generic[_MessageT]):
def __init__(self: BytesFeedParser[Message], _factory: None = ..., *, policy: Policy = ...) -> None: ...
@overload
def __init__(self, _factory: Callable[[], _MessageT], *, policy: Policy = ...) -> None: ...
def feed(self, data: bytes) -> None: ...
def feed(self, data: bytes | bytearray) -> None: ...
def close(self) -> _MessageT: ...
7 changes: 4 additions & 3 deletions stdlib/email/header.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections.abc import Iterable
from email.charset import Charset
from typing import Any

Expand All @@ -6,14 +7,14 @@ __all__ = ["Header", "decode_header", "make_header"]
class Header:
def __init__(
self,
s: bytes | str | None = ...,
s: bytes | bytearray | str | None = ...,
charset: Charset | str | None = ...,
maxlinelen: int | None = ...,
header_name: str | None = ...,
continuation_ws: str = ...,
errors: str = ...,
) -> None: ...
def append(self, s: bytes | str, charset: Charset | str | None = ..., errors: str = ...) -> None: ...
def append(self, s: bytes | bytearray | str, charset: Charset | str | None = ..., errors: str = ...) -> None: ...
def encode(self, splitchars: str = ..., maxlinelen: int | None = ..., linesep: str = ...) -> str: ...
def __eq__(self, other: object) -> bool: ...
def __ne__(self, __other: object) -> bool: ...
Expand All @@ -23,7 +24,7 @@ class Header:
# contains at least one encoded part.
def decode_header(header: Header | str) -> list[tuple[Any, Any | None]]: ...
def make_header(
decoded_seq: list[tuple[bytes, str | None]],
decoded_seq: Iterable[tuple[bytes | bytearray | str, str | None]],
maxlinelen: int | None = ...,
header_name: str | None = ...,
continuation_ws: str = ...,
Expand Down
2 changes: 1 addition & 1 deletion stdlib/email/message.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ __all__ = ["Message", "EmailMessage"]

_T = TypeVar("_T")

_PayloadType: TypeAlias = list[Message] | str | bytes
_PayloadType: TypeAlias = list[Message] | str | bytes | bytearray
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change means that get_payload() can suddenly return bytearray, which contradicts the documentation. set_payload() is accepting a bytearray, though (and converting it to a str).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_payload() returns Any in our stub, _PayloadType is only mentioned in a comment. I think the comment is still close enough, we can revisit it if we ever change the Any.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason I totally missed the comment in front of the return type ...

_CharsetType: TypeAlias = Charset | str | None
_HeaderType: TypeAlias = Any

Expand Down
2 changes: 1 addition & 1 deletion stdlib/email/mime/application.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ __all__ = ["MIMEApplication"]
class MIMEApplication(MIMENonMultipart):
def __init__(
self,
_data: str | bytes,
_data: str | bytes | bytearray,
_subtype: str = ...,
_encoder: Callable[[MIMEApplication], object] = ...,
*,
Expand Down
2 changes: 1 addition & 1 deletion stdlib/email/mime/audio.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ __all__ = ["MIMEAudio"]
class MIMEAudio(MIMENonMultipart):
def __init__(
self,
_audiodata: str | bytes,
_audiodata: str | bytes | bytearray,
_subtype: str | None = ...,
_encoder: Callable[[MIMEAudio], object] = ...,
*,
Expand Down
2 changes: 1 addition & 1 deletion stdlib/email/mime/image.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ __all__ = ["MIMEImage"]
class MIMEImage(MIMENonMultipart):
def __init__(
self,
_imagedata: str | bytes,
_imagedata: str | bytes | bytearray,
_subtype: str | None = ...,
_encoder: Callable[[MIMEImage], object] = ...,
*,
Expand Down
2 changes: 1 addition & 1 deletion stdlib/email/parser.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class HeaderParser(Parser): ...
class BytesParser:
def __init__(self, _class: Callable[[], Message] = ..., *, policy: Policy = ...) -> None: ...
def parse(self, fp: BinaryIO, headersonly: bool = ...) -> Message: ...
def parsebytes(self, text: bytes, headersonly: bool = ...) -> Message: ...
def parsebytes(self, text: bytes | bytearray, headersonly: bool = ...) -> Message: ...

class BytesHeaderParser(BytesParser): ...
12 changes: 7 additions & 5 deletions stdlib/email/quoprimime.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections.abc import Iterable

__all__ = [
"body_decode",
"body_encode",
Expand All @@ -13,11 +15,11 @@ __all__ = [

def header_check(octet: int) -> bool: ...
def body_check(octet: int) -> bool: ...
def header_length(bytearray: bytes) -> int: ...
def body_length(bytearray: bytes) -> int: ...
def unquote(s: str | bytes) -> str: ...
def quote(c: str | bytes) -> str: ...
def header_encode(header_bytes: bytes, charset: str = ...) -> str: ...
def header_length(bytearray: Iterable[int]) -> int: ...
def body_length(bytearray: Iterable[int]) -> int: ...
def unquote(s: str | bytes | bytearray) -> str: ...
def quote(c: str | bytes | bytearray) -> str: ...
def header_encode(header_bytes: bytes | bytearray, charset: str = ...) -> str: ...
def body_encode(body: str, maxlinelen: int = ..., eol: str = ...) -> str: ...
def decode(encoded: str, eol: str = ...) -> str: ...
def header_decode(s: str) -> str: ...
Expand Down
0