Skip to content

Commit c04a905

Browse files
author
Tim Riddermann
committed
use PEP688 buffer protocol for "parse" Method (fixes #24)
1 parent c6bded5 commit c04a905

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

construct-stubs/core.pyi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ from construct.lib import (
1717
ListType,
1818
RebufferedBytesIO,
1919
)
20+
from typing_extensions import Buffer
2021

2122
# unfortunately, there are a few duplications with "typing", e.g. Union and Optional, which is why the t. prefix must be used everywhere
2223

@@ -25,6 +26,7 @@ from construct.lib import (
2526
# - Higher Kinded Types: https://github.com/python/typing/issues/548
2627
# - Higher Kinded Types: https://sobolevn.me/2020/10/higher-kinded-types-in-python
2728

29+
ReadableBuffer: t.TypeAlias = Buffer
2830
StreamType = t.IO[bytes]
2931
FilenameType = t.Union[str, bytes, os.PathLike[str], os.PathLike[bytes]]
3032
PathType = str
@@ -95,7 +97,7 @@ class Construct(t.Generic[ParsedType, BuildTypes]):
9597
docs: str
9698
flagbuildnone: bool
9799
parsed: t.Optional[t.Callable[[ParsedType, Context], None]]
98-
def parse(self, data: bytes, **contextkw: ContextKWType) -> ParsedType: ...
100+
def parse(self, data: ReadableBuffer, **contextkw: ContextKWType) -> ParsedType: ...
99101
def parse_stream(
100102
self, stream: StreamType, **contextkw: ContextKWType
101103
) -> ParsedType: ...
@@ -113,7 +115,9 @@ class Construct(t.Generic[ParsedType, BuildTypes]):
113115
def compile(
114116
self, filename: FilenameType = ...
115117
) -> Construct[ParsedType, BuildTypes]: ...
116-
def benchmark(self, sampledata: bytes, filename: FilenameType = ...) -> str: ...
118+
def benchmark(
119+
self, sampledata: ReadableBuffer, filename: FilenameType = ...
120+
) -> str: ...
117121
def export_ksy(
118122
self, schemaname: str = ..., filename: FilenameType = ...
119123
) -> str: ...

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
url="https://github.com/timrid/construct-typing",
2222
author="Tim Riddermann",
2323
python_requires=">=3.7",
24-
install_requires=["construct==2.10.68"],
24+
install_requires=[
25+
"construct==2.10.68",
26+
"typing_extensions>=4.6.0"
27+
],
2528
keywords=[
2629
"construct",
2730
"kaitai",

0 commit comments

Comments
 (0)