Skip to content

Add more subscriptable types#44

Open
timrid wants to merge 1 commit into
mainfrom
add-subconstruct
Open

Add more subscriptable types#44
timrid wants to merge 1 commit into
mainfrom
add-subconstruct

Conversation

@timrid

@timrid timrid commented Jul 22, 2026

Copy link
Copy Markdown
Owner

Added Subconstruct, SymmetricAdapter, Tunnel and Validotor to construct_typed as subscriptable types.

@timrid timrid changed the title typed: Add more subscriptable types Add more subscriptable types Jul 22, 2026
…tor` to `construct_typed` as subscriptable types.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends construct_typed’s runtime-generic wrappers so additional construct base classes can be used as subscriptable generic types (e.g., Tunnel[...], Validator[...]) while keeping type-checker behavior consistent with the stubs.

Changes:

  • Exported new runtime-subscriptable generic wrappers: Subconstruct, SymmetricAdapter, Tunnel, and Validator.
  • Added tests asserting runtime subscriptability for several wrapper types.
  • Documented the new exports in the changelog.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
construct_typed/generic_wrapper.py Adds runtime generic wrapper classes for additional construct types and documents wrapper purpose.
construct_typed/__init__.py Re-exports the new wrapper types as part of the public API.
tests/test_typed.py Adds runtime subscriptability tests for newly supported generic wrappers (with gaps noted in comments).
CHANGELOG.md Notes the new subscriptable wrapper types under [UNRELEASED].

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -24,24 +28,48 @@

# at runtime, the original classes are no generics, so whe have to make new classes with generics support
Comment thread tests/test_typed.py
Comment on lines +808 to +820
def test_symmetric_adapter_subscriptable() -> None:
"""SymmetricAdapter has to be subscriptable at runtime."""

T = t.TypeVar("T")

class ReversedList(cst.SymmetricAdapter[list[T], list[T], list[T], list[T]]):
def _decode(
self, obj: list[T], context: cst.Context, path: cst.PathType
) -> list[T]:
return list(reversed(obj))

assert ReversedList(cs.Array(4, cs.Byte)).build([1, 2, 3, 4]) == b"\x04\x03\x02\x01"
assert ReversedList(cs.Array(4, cs.Byte)).parse(b"\x01\x02\x03\x04") == [4, 3, 2, 1]
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.

2 participants