Closed
Description
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
Labels
No labels