Skip to content

Commit

Permalink
use PEP688 buffer protocol for "parse" Method (fixes #24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Riddermann committed Jul 24, 2023
1 parent c6bded5 commit c04a905
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 6 additions & 2 deletions construct-stubs/core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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: ...
Expand All @@ -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: ...
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
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",
Expand Down

0 comments on commit c04a905

Please sign in to comment.