Skip to content

Implement PEP 688 #10224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JelleZijlstra opened this issue May 26, 2023 · 1 comment · Fixed by #10225
Closed

Implement PEP 688 #10224

JelleZijlstra opened this issue May 26, 2023 · 1 comment · Fixed by #10225

Comments

@JelleZijlstra
Copy link
Member

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.

@srittau
Copy link
Collaborator

srittau commented May 26, 2023

Sounds like a good plan. While I usually don't like lying in the stubs, this is only temporary and (most likely) an overall improvement.

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 a pull request may close this issue.

2 participants