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

Add support for buffer protocol (PEP 688) #10

Merged
merged 2 commits into from
May 20, 2024
Merged

Add support for buffer protocol (PEP 688) #10

merged 2 commits into from
May 20, 2024

Conversation

Rogdham
Copy link
Owner

@Rogdham Rogdham commented May 20, 2024

With PEP 688 being accepted and included in Python 3.12, we should accept and test for custom classes implementing the buffer protocol being passed to Parser.

Moreover, static type checkers will stop promoting bytearray/memoryview/etc. to bytes at some point, so let's anticipate that (has been tested with custom version of mypy).

Mypy patch

diff --git a/mypy/options.py b/mypy/options.py
index 5ef6bc2a3..2642b6407 100644
--- a/mypy/options.py
+++ b/mypy/options.py
@@ -375,8 +375,8 @@ class Options:
         # (undocumented feature).
         self.export_ref_info = False
 
-        self.disable_bytearray_promotion = False
-        self.disable_memoryview_promotion = False
+        self.disable_bytearray_promotion = True
+        self.disable_memoryview_promotion = True
         self.force_uppercase_builtins = False
         self.force_union_syntax = False

Errors messages in current version with patched mypy

tests/unit/test_stream.py: note: In function "test_chain_types":
tests/unit/test_stream.py:107: error: Argument 2 to "StreamChain" has incompatible type "bytearray"; expected "Streamable"  [arg-type]
tests/unit/test_stream.py:107: note: Following member(s) of "bytearray" have conflicts:
tests/unit/test_stream.py:107: note:     Expected:
tests/unit/test_stream.py:107: note:         def __iter__(self) -> Iterator[Streamable]
tests/unit/test_stream.py:107: note:     Got:
tests/unit/test_stream.py:107: note:         def __iter__(self) -> Iterator[int]
tests/unit/test_stream.py:108: error: Argument 3 to "StreamChain" has incompatible type "memoryview"; expected "Streamable"  [arg-type]
tests/unit/test_stream.py:108: note: Following member(s) of "memoryview" have conflicts:
tests/unit/test_stream.py:108: note:     Expected:
tests/unit/test_stream.py:108: note:         def __iter__(self) -> Iterator[Streamable]
tests/unit/test_stream.py:108: note:     Got:
tests/unit/test_stream.py:108: note:         def __iter__(self) -> Iterator[int]
Found 2 errors in 1 file (checked 26 source files)

Change typing-extensions requirement to provide Buffer class for Python
versions up to 3.11.
The dependency is now installed for Python 3.10 and 3.11 as well, allowing to
use it in the codebase for those versions.
@Rogdham Rogdham merged commit 76fd313 into master May 20, 2024
15 checks passed
@Rogdham Rogdham deleted the pep-688 branch May 20, 2024 13:55
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.

1 participant