Add more subscriptable types#44
Open
timrid wants to merge 1 commit into
Open
Conversation
…tor` to `construct_typed` as subscriptable types.
There was a problem hiding this comment.
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, andValidator. - 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 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Added
Subconstruct,SymmetricAdapter,TunnelandValidotortoconstruct_typedas subscriptable types.