-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Generic Structs #386
Conversation
Moving the type info into a separate container is necessary to make Generic structs work.
Also fixes a few bugs
Maybe this will fix the sudden gcov failure?
Some change in `setuptools` or `pip` seems to have dropped the ability to retain the build directory, which we need for generating coverage of the .c files. I've spent a bit of time trying to make this work with `pip install -e .` and can't seem to get things to work. We'll use the deprecated command for now.
Alright, things are passing now, merging 🚀. I hope to cut a release containing generics support in the first week of May. If anyone is interested, I'd love it if someone could install from the main branch (https://jcristharif.com/msgspec/install.html#installing-from-github) and try things out. I believe our test suite should cover all the possible edge cases, but if there are bugs it'd be nice to catch 'em now before the release. |
^^^ cc @provinzkraut since you mentioned you were interested in generic support here, and generally provide excellent feedback :). |
This adds full support for encoding/decoding generic
msgspec.Struct
types. Fixes #193.A quick demo:
Recursive and complicated parametrizations also should work, including the weird challenge posted on the pydantic twitter:
In common usage, generics should have no runtime overhead. Since msgspec doesn't validate on
__init__
we don't need to generate a new type for each parametrization - type type ofPaginated[Order]
istyping._GenericAlias
. No magic on our side needed!The scoping rules and behavior for handling complicated parametrized generics are underdocumented in the corresponding PEPs. I believe we've covered all the edge cases here, but would still love for a few users to try this out before it's released. If this passes tests I plan on merging it early, but would hope someone could try this out before it's released.