-
Notifications
You must be signed in to change notification settings - Fork 118
Expose and Enable Inheritance of StructMeta Metaclass #843
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
Conversation
…ns like asdict/json to handle structs created by StructMeta subclasses
|
@nightsailer I sure hope this passes you don't know how annoying it has been to try and turn Msgspec into an Object Relational Mapping Library. If this passes it would likely remedy my recent problems #820 and possibly allow StructMeta and any SQLAlchemy Type Metaclass object to be theoretically mergeable similar to how SQLModel works. |
|
Also I ran your test on my computer and it is working correctly. |
|
Ah, hey @nightsailer, seems like we were working in parallel! See my PR #844. [You were first, I did not re-check for open PRs :)] Our changes are quite similar, except that
May I make one commit to your PR to update the stub for StructMeta to this one? |
Sure, absolutely! Please feel free to add that commit to update the stub. Thanks for offering! By the way, the reason for updating the check from |
|
Ok that makes sense, thanks for clarifying! Please merge this PR for the change we discussed: nightsailer#1 |
Added `StructMeta` stub to `__init__.pyi`
Seconded. I am working on an ORM and want to set msgspec.UNSET to everything by default in the metaclass, so that I can have quick and dirty DTOs on the cheap. But currently it is verbose and annoying having to manually annotate every model with |
|
@jcrist I think there's a large amount of interest in this PR! It would be really wonderful if you could take a look and consider merging. Thanks so much. |
I'm interested because it means I would be able to add msgspec to a few libraries such as a few that I currently maintain such as aiocallback and a few others. |
|
I have fork a new package: msgspec-x, this pr was conflict, close now. |
|
Hey @nightsailer, while you've made a new package with this feature, I feel like the existing package has many pre-existing users and also deserves to have this feature. Would you consider re-opening this PR? Thanks. |
|
@nightsailer given that there seems to be a transition to new mantainers, could you open a new PR with the original changes so that these can be merged into msgspec? |
|
I'd be eager to merge this if you or someone else wouldn't mind opening another PR 😄 |
Expose and Enable Inheritance of StructMeta Metaclass
Changes
StructMetametaclass to Python for direct usePy_TPFLAGS_BASETYPEflag to enable Python inheritance ofStructMetaWhy
This allows users to:
StructMetadirectly without subclassingStructStructMetabehaviorThis PR resolves:
kw_onlyforStruct(e.g.,kw_only_default=True) #841msgspec.Struct#837Example
Technical Details
Added StructMeta to module exports in _core.c
Added Py_TPFLAGS_BASETYPE flag to StructMetaType
Updated init.py to import and expose StructMeta
Added type hints in init.pyi
Compatibility
These changes are backward compatible and only add new capabilities without modifying existing behavior.
Related Issues