Skip to content

Implement PEP 688 #10224

Closed
Closed
@JelleZijlstra

Description

@JelleZijlstra

PEP 688 has been accepted and implemented in Python 3.12. For Python 3.12 and up, this provides a clean, protocol-based way to implement buffers.

Unfortunately, we still can't directly represent buffer types on earlier Python versions. I'd like to propose the following approach:

  • We pretend that typing_extensions.Buffer is a Protocol with a __buffer__ method even before 3.12.
  • We create __buffer__ methods on buffer classes without a version condition, even though the method does not actually exist before 3.12.
  • Now, we can define protocols that combine __buffer__ with other methods, such as the current _typeshed.SliceableBuffer.

The downside is that we're lying about the existence of the __buffer__ method, so that type checkers will not catch places where users call the method on Python < 3.12. However, hopefully this will be rare, and it is outweighed by the benefit that we can now talk about arbitrary buffer types, including third-party ones, without the big unions we're currently using in _typeshed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions