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

email: improve bytes handling #9032

Merged
merged 3 commits into from
Nov 1, 2022
Merged

email: improve bytes handling #9032

merged 3 commits into from
Nov 1, 2022

Conversation

JelleZijlstra
Copy link
Member

No description provided.

@github-actions

This comment has been minimized.

def decode(string: str | bytes) -> bytes: ...
from _typeshed import ReadableBuffer

def header_length(bytearray: bytes | bytearray) -> int: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

str would work as well, and since header_encode() explicitly checks for str, I think it's safer to allow it here as well. In fact, the tests use str:

https://github.com/python/cpython/blob/df7c8b95372169fb9d23140d35f91970ba32189d/Lib/test/test_email/test_email.py#L4324

Suggested change
def header_length(bytearray: bytes | bytearray) -> int: ...
def header_length(bytearray: str | bytes | bytearray) -> int: ...

Copy link
Member Author

Choose a reason for hiding this comment

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

My thinking was that this function is about counting bytes using len(), and that doesn't make sense for str because a character can be multiple bytes. I guess it works for ASCII strings, though.

Copy link
Collaborator

Choose a reason for hiding this comment

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

At first I was not going to comment this for that reason, but seeing the test using str tipped me to comment on this.

@@ -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 ...

@github-actions
Copy link
Contributor

github-actions bot commented Nov 1, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau merged commit 7ab933f into python:master Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants