From 091c204b0a19bea3e9a4ffebdc69750117843220 Mon Sep 17 00:00:00 2001 From: "Robin.Schmidt" Date: Fri, 14 Aug 2026 11:39:27 +0200 Subject: [PATCH] Fixed typing of `Construct._build()`. --- CHANGELOG.md | 6 +++++- construct-stubs/core.pyi | 4 +++- tests/test_core.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e88943a..9749361 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [Unreleased] +**Fixes:** +- Fixed return type of `Construct._build()`: `int` was wrong and worked only in specific cases. + ## [0.8.1] - 2026-07-23 **New features:** - Added `Subconstruct`, `SymmetricAdapter`, `Tunnel` and `Validator` to `construct_typed` as subscriptable types. @@ -23,4 +27,4 @@ - Use PEP604 union syntax "X | Y" instead of "Union[X, Y]" and "X | None" instead of "Optional[X]" in type hints. **Organizational changes:** -- Use `uv` as a project management tool and `poe` as a task runner. \ No newline at end of file +- Use `uv` as a project management tool and `poe` as a task runner. diff --git a/construct-stubs/core.pyi b/construct-stubs/core.pyi index 361c2ab..66dffb4 100644 --- a/construct-stubs/core.pyi +++ b/construct-stubs/core.pyi @@ -164,9 +164,11 @@ class Construct(t.Generic[ParsedType, BuildTypes]): def _parsereport( self, stream: StreamType, context: Context, path: PathType ) -> ParsedType: ... + # In most implementations, `_build()` actually returns `ParsedType`, in some others it + # returns `BuildTypes` or something else entirely. `t.Any` seems a good compromise. def _build( self, obj: BuildTypes, stream: StreamType, context: Context, path: PathType - ) -> int: ... + ) -> t.Any: ... def _sizeof(self, context: Context, path: PathType) -> int: ... @t.type_check_only diff --git a/tests/test_core.py b/tests/test_core.py index aad3516..6e6b33b 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1781,6 +1781,16 @@ def __init__( hashfunc: t.Callable[[bytes], BuildTypes], bytesfunc: t.Callable[[Context], bytes], ) -> None: ... + + def _parse(self, stream: t.IO[bytes], context: Context, path: str) -> ParsedType: + ... + + def _build(self, obj: BuildTypes, stream: t.IO[bytes], context: Context, path: str) -> t.Any: + ... + + def _sizeof(self, context: Context, path: str) -> int: + ... + else: import binascii