Am 27.10.22 um 03:31 schrieb Jelle Zijlstra:
I recently revised PEP 688 (https://peps.python.org/pep-0688/), which proposes a mechanism to make the buffer protocol accessible to the type system. The most technically challenging part of the PEP is in the interaction with the C API, so I opened a primary discussion thread on the core dev Discourse at https://discuss.python.org/t/pep-688-take-2-making-the-buffer-protocol-accessible-in-python/19756.

However, the PEP also proposes a change that affects primarily static type checkers: removing the implicit promotion of memoryview and bytearray to bytes. For context, the CPython docs currently specify that a type annotation of "bytes" should also include bytearray and memoryview values, similar to how "float" implicitly includes int. Mypy and pyright implement this rule; pyre does not.

Speaking with my typeshed maintainer hat: If we are removing that promotion, we need a transition strategy. Here's my suggestion for typeshed:

  1. Introduce a TypeAlias `_typeshed.OldBytes = bytes | bytearray | memoryview` (or similar).
  2. Document that `OldBytes` must not be used manually.
  3. Programmatically replace all occurrences of "bytes" in argument types with `OldBytes`. (We can only do this for third-party stubs once all typecheckers include `_typeshed.OldBytes`. This should ideally happen, *before* typecheckers remove support for the automatic promotion.)
  4. Check all occurrences of `OldBytes` manually and replace them with the appropriate types. This can be done over time when touching a particular stub file anyway.
  5. Eventually (in a few years) remove `_typeshed.OldBytes`.

 - Sebastian