Skip to content

Conversation

@AlexWaygood
Copy link
Member

@github-actions
Copy link
Contributor

github-actions bot commented Apr 4, 2022

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

@JelleZijlstra
Copy link
Member

I don't feel great about losing precision here. This isn't even specified in PEP 484.

For instance, I just looked at builtins.pyi and the first usage of bytes I found was for ord(): def ord(__c: str | bytes) -> int: .... I looked at the implementation and it allows bytearray, but not memoryview.

Next is the constructor of UnicodeDecodeError. We have it as bytes, but it actually allows any buffer type (so also e.g. array.array).

Then int.__new__. We have one overload that takes bytes (among others) and another that takes bytes | bytearray. Checking the implementation, the first overload allows any buffer type and the second overload actually allows bytes and bytearray, but not memoryview.

I think we're better off reflecting the runtime reality exactly (using _typeshed.ReadableBuffer where appropriate), instead of pretending that these types are all the same.

@AlexWaygood
Copy link
Member Author

Thanks for calling me out. The typing docs seem very bad in this regard. According to this BPO issue, the intended meaning of the entry for ByteString is that "memoryview is sometimes a subtype of ByteString but not all the time". That wasn't obvious at all from my reading of the section, and I don't really understand how it can be put to practical use when using type hints.

Anyway, you're right that this approach is far too sweeping :)

@AlexWaygood AlexWaygood closed this Apr 4, 2022
@AlexWaygood AlexWaygood deleted the bytes branch April 4, 2022 22:12
JelleZijlstra added a commit to JelleZijlstra/typeshed that referenced this pull request Apr 16, 2022
As a followup from python#7589 (comment),
I audited all occurrences of bytes in builtins.pyi by reading the corresponding C code
on CPython main.

Most use the C buffer protocol, so _typeshed.ReadableBuffer is the right type. A few
check specifically for bytes and bytearray.
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