Skip to content
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

make Buffer compatible with python 3.10 and above #144

Closed
wants to merge 1 commit into from

Conversation

Aiqin-Aiven
Copy link

Fix the error on pghoard pipeline for python 3.10 and above
E ImportError: cannot import name 'Buffer' from 'typing_extensions' (/opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/typing_extensions.py)

Why this way

In Python 3.10 and later, the Buffer name is no longer provided by typing_extensions. Instead, you can use typing.ByteString or typing.MutableSequence[bytes] as a replacement for Buffer.

@@ -205,10 +213,10 @@ def seek(self, offset: int, whence: int = 0) -> int:
def truncate(self, size: Optional[int] = None) -> int:
raise UnsupportedOperation("truncate")

def write(self, s: Union[bytes, Buffer]) -> int:
def write(self, s: Union[bytes, Buffer]) -> int: # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need to ignore the type here (and in writelines)?

Copy link
Contributor

Choose a reason for hiding this comment

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

BinaryIO wants only bytes and no Buffer as input for write since it's basically just IO[bytes], but I believe in some places we actually do want to forward some buffer instead of bytes hence why Buffer appears here.

I believe that currently there is no way to avoid ignoring some cases because we have some dependencies that want BinaryIO and other places that don't want that in their typing. So if we remove Buffer here and make this a "proper" BinaryIO then you'll find similar comments when we use this class instead.

Copy link
Author

Choose a reason for hiding this comment

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

I believe we don't have other better option here, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I don't think we have better options now. Unfortunately this area of typing is a bit of a mess...

Copy link
Contributor

Choose a reason for hiding this comment

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

The comment is not blocking from my side, was just trying to understand the issue. TY @giacomo-alzetta-aiven for breaking it down. The only concern was that it might hide legit issues that can pop up in the future. Maybe even that is overblown? I don't know.

@codecov-commenter
Copy link

codecov-commenter commented Sep 22, 2023

Codecov Report

Merging #144 (647ad9f) into main (1769e1b) will increase coverage by 0.04%.
Report is 2 commits behind head on main.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #144      +/-   ##
==========================================
+ Coverage   66.38%   66.42%   +0.04%     
==========================================
  Files          35       35              
  Lines        3879     3884       +5     
==========================================
+ Hits         2575     2580       +5     
  Misses       1304     1304              
Files Changed Coverage Δ
rohmu/util.py 74.82% <100.00%> (+0.74%) ⬆️

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@rominf rominf left a comment

Choose a reason for hiding this comment

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

Everything is much easier:

  1. Buffer was introduced to typing_extensions in 4.6.0: https://github.com/python/typing_extensions/blob/main/CHANGELOG.md#release-460-may-22-2023
  2. pghoard pins typing_extensions==4.5.0: https://github.com/Aiven-Open/pghoard/blob/21bc21446977cadd2ac42e967bbba846d930f9d5/constraints.txt#L78
  3. typing_extensions.Buffer supports all Python versions: https://github.com/python/typing_extensions/blob/c17c499b865585458bc334a1f895ebaedd2ab854/src/typing_extensions.py#L2522-L2548
  4. So, you need just bump typing_extensions in pghoard to at least 4.6.0. I would bump it to 4.7.1, since it's what we currently have downstream.

@Aiqin-Aiven
Copy link
Author

As per: Aiven-Open/pghoard#601
close this PR for now.

@exg exg deleted the aiqin-rr branch December 22, 2023 13:10
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.

5 participants