You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unexpected type errors with bytes and typing.ByteString #4871
I've run into some issues when using bytes or typing.ByteString as a way to annotate arguments and return types that are able to be bytes, bytearray, or memoryview.
$ mypy test.py
test.py:4: error: Incompatible return value type (got "Sequence[int]", expected "ByteString")
test.py:16: error: Argument 1 to "bytestring_test" has incompatible type "memoryview"; expected "ByteString"
test.py:19: error: Argument 1 to "bytes_test" has incompatible type "memoryview"; expected "bytes"
I expect there to be no type errors at all, but please correct me if my understanding is flawed.
If you ask me, ByteString never really came off the ground. The code in the stdlib has a comment
XXX Should add all their methods.
The types.ByteString class is just an alias for collections.abc.ByteString, and it typeshed it's an empty subclass of Sequence[int].
If you wanted to change all that you'd have to file issues on bugs.python.org and debate the merit.
The proper way to type these things for mypy is to just use bytes -- mypy already treats bytearray as a virtual subclass of bytes. Probably it should do the same for memoryview (though it doesn't come up often enough to matter much in practice). See also python/typing#552
Activity
gvanrossum commentedon Apr 9, 2018
If you ask me,
ByteStringnever really came off the ground. The code in the stdlib has a commentThe
types.ByteStringclass is just an alias forcollections.abc.ByteString, and it typeshed it's an empty subclass ofSequence[int].If you wanted to change all that you'd have to file issues on bugs.python.org and debate the merit.
The proper way to type these things for mypy is to just use
bytes-- mypy already treatsbytearrayas a virtual subclass ofbytes. Probably it should do the same formemoryview(though it doesn't come up often enough to matter much in practice). See also python/typing#552ericnorris commentedon Apr 9, 2018
I'm fine with just using
bytes, but as you noted it does not work withmemoryview. I'll follow the progress on the other ticket. Thanks!ByteString is considered harmful
replace `ByteString` type annotations with `bytes`