From c04a90575d5e68097ed455f918c01e89f1b3d744 Mon Sep 17 00:00:00 2001 From: Tim Riddermann Date: Mon, 24 Jul 2023 09:58:17 +0200 Subject: [PATCH] use PEP688 buffer protocol for "parse" Method (fixes #24) --- construct-stubs/core.pyi | 8 ++++++-- setup.py | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/construct-stubs/core.pyi b/construct-stubs/core.pyi index 34aaf79..4a28191 100644 --- a/construct-stubs/core.pyi +++ b/construct-stubs/core.pyi @@ -17,6 +17,7 @@ from construct.lib import ( ListType, RebufferedBytesIO, ) +from typing_extensions import Buffer # unfortunately, there are a few duplications with "typing", e.g. Union and Optional, which is why the t. prefix must be used everywhere @@ -25,6 +26,7 @@ from construct.lib import ( # - Higher Kinded Types: https://github.com/python/typing/issues/548 # - Higher Kinded Types: https://sobolevn.me/2020/10/higher-kinded-types-in-python +ReadableBuffer: t.TypeAlias = Buffer StreamType = t.IO[bytes] FilenameType = t.Union[str, bytes, os.PathLike[str], os.PathLike[bytes]] PathType = str @@ -95,7 +97,7 @@ class Construct(t.Generic[ParsedType, BuildTypes]): docs: str flagbuildnone: bool parsed: t.Optional[t.Callable[[ParsedType, Context], None]] - def parse(self, data: bytes, **contextkw: ContextKWType) -> ParsedType: ... + def parse(self, data: ReadableBuffer, **contextkw: ContextKWType) -> ParsedType: ... def parse_stream( self, stream: StreamType, **contextkw: ContextKWType ) -> ParsedType: ... @@ -113,7 +115,9 @@ class Construct(t.Generic[ParsedType, BuildTypes]): def compile( self, filename: FilenameType = ... ) -> Construct[ParsedType, BuildTypes]: ... - def benchmark(self, sampledata: bytes, filename: FilenameType = ...) -> str: ... + def benchmark( + self, sampledata: ReadableBuffer, filename: FilenameType = ... + ) -> str: ... def export_ksy( self, schemaname: str = ..., filename: FilenameType = ... ) -> str: ... diff --git a/setup.py b/setup.py index 54dd52d..aa7b612 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,10 @@ setup( url="https://github.com/timrid/construct-typing", author="Tim Riddermann", python_requires=">=3.7", - install_requires=["construct==2.10.68"], + install_requires=[ + "construct==2.10.68", + "typing_extensions>=4.6.0" + ], keywords=[ "construct", "kaitai",