Skip to content

Update mmap stubs for newer Python versions - #4244

Merged
JelleZijlstra merged 2 commits into
python:masterfrom
bmerry:mmap-readable-buffer
Jun 21, 2020
Merged

JelleZijlstra merged 2 commits into
python:masterfrom
bmerry:mmap-readable-buffer

Conversation

@bmerry

@bmerry bmerry commented Jun 19, 2020

Copy link
Copy Markdown
Contributor

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 Introduce ReadableBuffer and WriteableBuffer Union aliases #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.

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

bmerry commented Jun 19, 2020

Copy link
Copy Markdown
Contributor Author

Incidentally, I don't really follow why master defines rfind separately for Python 2/3 while it defines find in the base class. I'd be happy to clean that up too if there is no good reason for it.

Comment thread stdlib/2and3/mmap.pyi Outdated
Comment on lines +66 to +70
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: ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since typeshed only supports Python 2.7 and 3.5+, you can leave out the version check and the else branch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I've used that to simplify/clean up things a bit further.

@srittau

srittau commented Jun 19, 2020

Copy link
Copy Markdown
Collaborator

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 git log and git blame for the file in question can also give hints.

bmerry added a commit to bmerry/typeshed that referenced this pull request Jun 19, 2020
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 JelleZijlstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@JelleZijlstra
JelleZijlstra merged commit ec6116d into python:master Jun 21, 2020
@bmerry
bmerry deleted the mmap-readable-buffer branch June 22, 2020 06:56
@bmerry

bmerry commented Jun 22, 2020

Copy link
Copy Markdown
Contributor Author

I'm wondering what's going on with the "_mmap" class.

Glad it's not just me that's confused. I'm pretty tempted to just reorganise it as a single class with if sys_version blocks.

JelleZijlstra pushed a commit that referenced this pull request Jun 22, 2020
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.
vishalkuo pushed a commit to vishalkuo/typeshed that referenced this pull request Jun 26, 2020
* 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.
vishalkuo pushed a commit to vishalkuo/typeshed that referenced this pull request Jun 26, 2020
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.
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.

3 participants