Repository navigation
Update mmap stubs for newer Python versions - #4244
Conversation
Based on the Python stdlib documentation:
- Since Python 3.5, mmap.{find,rfind,write} all accept any bytes-like.
I've used the _typeshed.ReadableBuffer alias defined in python#4232.
- Since Python 3.6, mmap.write returns the number of bytes written.
- Since Python 3.3, mmap.read allows None as the parameter; while in
Python 2 the argument cannot be omitted.
|
Incidentally, I don't really follow why master defines |
| if sys.version_info >= (3, 5): | ||
| def find(self, sub: ReadableBuffer, start: int = ..., stop: int = ...) -> int: ... | ||
| def rfind(self, sub: ReadableBuffer, start: int = ..., stop: int = ...) -> int: ... | ||
| else: | ||
| def rfind(self, sub: bytes, start: int = ..., stop: int = ...) -> int: ... |
There was a problem hiding this comment.
Since typeshed only supports Python 2.7 and 3.5+, you can leave out the version check and the else branch.
There was a problem hiding this comment.
Thanks, I've used that to simplify/clean up things a bit further.
|
Generally, some type stubs are rather old and use conventions that predate modern typeshed conventions. They also often have been patched several times by multiple people. So if you find something amiss, it's often due to these factors, and cleanups are most welcome. Checking the output of |
This is a follow-up on python#4232. memoryview, hashlib, and hmac are updated to use ReadableBuffer type instead of their own home-spun unions of bytes, bytearray and whatever else each use case used. mmap is being handled in python#4244, and I'll leave BinaryIO for another day (or possibly another person) because it's going to require some messy code duplication because the relevant methods are defined in IO[AnyStr]. There's one corner case I'm not quite sure how best to handle: the documentation for hmac.digest claim that the parmaeters have the same meanings as in hmac.new, but in CPython the latter has an explicit check that `key` is bytes or bytearray while the former works with a memory-view. For now I've matched the documentation. Also, the documentation for HMAC.update says that `msg` can be any type supported by hashlib from Python 3.4; but I can't see anything in the Python 2.7 implementation that would prevent it also taking bytes-like objects, so I've not tried to treat Python 2 any different to Python 3.
Use the fact that Python 3.0-3.4 are no longer supported to clean up the version-dependent logic. Functions that always have different signatures in Python 2/3 are moved from the base _mmap[bytes] to the mmap subclass.
JelleZijlstra
left a comment
There was a problem hiding this comment.
This all looks correct.
I'm wondering what's going on with the "_mmap" class. It doesn't exist at runtime in either Python 2 or 3—the mmap.mmap type inherits only from object. Maybe it exists so that we can share some methods between Python 2 and 3, but that doesn't explain why it's generic over AnyStr.
Glad it's not just me that's confused. I'm pretty tempted to just reorganise it as a single class with |
This is a follow-up on #4232. memoryview, hashlib, and hmac are updated to use ReadableBuffer type instead of their own home-spun unions of bytes, bytearray and whatever else each use case used. mmap is being handled in #4244, and I'll leave BinaryIO for another day (or possibly another person) because it's going to require some messy code duplication because the relevant methods are defined in IO[AnyStr]. There's one corner case I'm not quite sure how best to handle: the documentation for hmac.digest claim that the parmaeters have the same meanings as in hmac.new, but in CPython the latter has an explicit check that `key` is bytes or bytearray while the former works with a memory-view. For now I've matched the documentation. Also, the documentation for HMAC.update says that `msg` can be any type supported by hashlib from Python 3.4; but I can't see anything in the Python 2.7 implementation that would prevent it also taking bytes-like objects, so I've not tried to treat Python 2 any different to Python 3.
* Update mmap stubs for newer Python versions
Based on the Python stdlib documentation:
- Since Python 3.5, mmap.{find,rfind,write} all accept any bytes-like.
I've used the _typeshed.ReadableBuffer alias defined in python#4232.
- Since Python 3.6, mmap.write returns the number of bytes written.
- Since Python 3.3, mmap.read allows None as the parameter; while in
Python 2 the argument cannot be omitted.
* Further clean up mmap.pyi
Use the fact that Python 3.0-3.4 are no longer supported to clean up the
version-dependent logic. Functions that always have different signatures
in Python 2/3 are moved from the base _mmap[bytes] to the mmap subclass.
This is a follow-up on python#4232. memoryview, hashlib, and hmac are updated to use ReadableBuffer type instead of their own home-spun unions of bytes, bytearray and whatever else each use case used. mmap is being handled in python#4244, and I'll leave BinaryIO for another day (or possibly another person) because it's going to require some messy code duplication because the relevant methods are defined in IO[AnyStr]. There's one corner case I'm not quite sure how best to handle: the documentation for hmac.digest claim that the parmaeters have the same meanings as in hmac.new, but in CPython the latter has an explicit check that `key` is bytes or bytearray while the former works with a memory-view. For now I've matched the documentation. Also, the documentation for HMAC.update says that `msg` can be any type supported by hashlib from Python 3.4; but I can't see anything in the Python 2.7 implementation that would prevent it also taking bytes-like objects, so I've not tried to treat Python 2 any different to Python 3.
Based on the Python stdlib documentation:
I've used the _typeshed.ReadableBuffer alias defined in Introduce ReadableBuffer and WriteableBuffer Union aliases #4232.
Python 2 the argument cannot be omitted.