As evidenced by my message to typing-sig (apologies, sent before discussions-to was updated), I’m sympathetic to concerns about breaking bytes / bytearray compatibility. However, I find Jelle’s point about hashability fairly compelling — and in general it’s nice to be able to reflect immutability in one’s types.
but I don’t want to get too precise or restrictive here
Part of the issue is that using bytes
as a shorthand prevents people who want to be precise from being precise. If we choose to remove the shorthand, maybe we could suggest use of typing.ByteString
as an “imprecise” type.
Breaking down type annotation use cases
A) buffers that are passed to C code that use the buffer protocol
B) functions that use all the various methods of builtins.bytes
(join, find, lower, etc) — these likely also work on bytearray
“for free”
C) functions that hash bytes
D) functions that iterate over bytes or e.g. extend a bytearray
In the current world (where static duck typing lets us treat bytearray
and memoryview
as subtypes of bytes
)
A) is not expressible
B) is typed as bytes
. hooray static duck typing, type checkers let us pass bytearray
and things work. but boo, static duck typing, we’re allowed to pass memoryview
and things don’t work.
C) is typed as bytes
. boo static duck typing, since type checkers let us pass bytearray
and writable memoryview
D) is often typed as bytes
, as shorthand for bytes | bytearray | memoryview
, but should probably be typed as Sequence[int]
In the PEP 688 world as proposed
A) is typed as types.Buffer
B) is typed as bytes | bytearray
C) is typed as bytes
, or maybe bytes | memoryview
if we’re willing to hope noone passes us a writable memoryview
D) probably should be typed as Sequence[int]
In a PEP 688 world but where we allow bytearray
to duck type to bytes
(but not memoryview
)
A) is typed as types.Buffer
B) is typed as bytes
C) is typed as bytes
but we risk someone passing us a bytearray
(or bytes | memoryview
, with similar caveat to above)
D) probably should be typed as Sequence[int]
Overall I think I could give or take duck type compatibility between bytearray
and bytes
, but I do strongly feel we should drop memoryview
from the bytes shorthand. The only non Sequence[int]
-like thing I can think of that works on bytes | bytearray | memoryview
is b.hex()