8000 email.message: Allow any header value by srittau · Pull Request #11574 · python/typeshed · GitHub
[go: up one dir, main page]

Skip to content

email.message: Allow any header value #11574

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 1 commit into from
Mar 11, 2024
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
10000 Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions stdlib/email/message.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ _PayloadType: TypeAlias = Message | str
_EncodedPayloadType: TypeAlias = Message | bytes
_MultipartPayloadType: TypeAlias = list[_PayloadType]
_CharsetType: TypeAlias = Charset | str | None
# Type returned by Policy.header_fetch_parse, AnyOf[str | Header]
# Type returned by Policy.header_fetch_parse, often str or Header.
_HeaderType: TypeAlias = Any
_HeaderTypeParam: TypeAlias = str | Header
# Type accepted by Policy.header_store_parse.
_HeaderTypeParam: TypeAlias = str | Header | Any
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't the complex payload type from get_payload the issue for #11095 (comment) ? (which I don't think is concerned with the header type)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, no idea why I though this was the issue. Still, this PR fixes another issue.

Copy link
Collaborator
@Avasam Avasam Mar 11, 2024

Choose a reason for hiding this comment

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

Since this is used exclusively for parameters/assignement, should it be

Suggested change
_HeaderTypeParam: TypeAlias = str | Header | Any
_HeaderTypeParam: TypeAlias = str | Header | object

Either way maybe also with a comment explaining the use of a redundant union here (even if it's just "read TODO on Message")

often str or Header.

eh, I think that's sufficiant

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

object wouldn't be correct, as it would imply that all values are correct, which isn't the case. We just can't express the accepted type properly.


class _SupportsEncodeToPayload(Protocol):
def encode(self, encoding: str, /) -> _PayloadType | _MultipartPayloadType | _SupportsDecodeToPayload: ...

class _SupportsDecodeToPayload(Protocol):
def decode(self, encoding: str, errors: str, /) -> _PayloadType | _MultipartPayloadType: ...

# TODO: This class should be generic over the header policy and/or the header
# value types allowed by the policy. This depends on PEP 696 support
# (https://github.com/python/typeshed/issues/11422).
class Message:
policy: Policy # undocumented
preamble: str | None
Expand Down
6 changes: 6 additions & 0 deletions test_cases/stdlib/email/check_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from email.headerregistry import Address
from email.message import EmailMessage

msg = EmailMessage()
msg["To"] = "receiver@example.com"
msg["From"] = Address("Sender Name", "sender", "example.com")
0