-
Notifications
You must be signed in to change notification settings - Fork 213
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 pydantic dataclasses #406
Conversation
The `to_json` method does not include the `include_default_values` option from `to_dict`. While the implementation to `to_json` is almost litteraly only a call to `to_dict()` (and thus trivial to replace), it is more convenient for users to not have to reimplement it when the option is desired.
I am pitching this as an additional feature, feel free to let me know what you think. I will maintain my own fork otherwise; I do feel like this is a trivial improvement to implement. edit |
I'll put as draft for now. Some issues to note:
Right now the imports are done through the template, but it might be cleaner to do the pydantic imports through the builtin import system. ForwardRefs are an easy fix, oneOf can be partially achieved through validators and overriding |
Plan for the
|
Changed the validator to ensure that exactly one field is defined. |
Further tasks
|
Pydantic dataclasses are a drop-in replacement for builtin dataclasses to offer a stronger type validation. This PR adds support for generating pydantic dataclasses instead of the builtin variant. This enable usage of protobuf classes in API endpoint (like FastAPI) which supports parsing pydantic objects but not builtin dataclasses.
Mypy support is a requirement for this project; keep the same level of support
Hey @danielgtaylor. I've completed these changes a while back. This code has been in production at Garda World for well over two months now without any specific incident. I just completed the unit test portion for windows (generation part). Let me know if this is something you are interested in having in the mainline version of python-betterproto. I did not add any specific test to protobuf but instead relied on use-cases; all previous tests were also ported so they are running on the protobuf version (insuring no regression) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine to me and seems like a safe addition, since the generation is simply controlled by an option.
Probably want to hear from another member to see what they think
Hi there 👋 Anything I can do to help here? 🙏 |
I'll try and take a look at this over the weekend |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good so far, it'd be nice if there was some documentation for this in the README.md instead of having to source dive for this
@SamuelYvon are you still interested in this? If not, I can take over. |
@Kludex Ill finish the requested changes today :) We are actively using this at Garda so I have allocated time to finish this feature. |
Thinking a bit ahead... Would there be a way to create validators and not lose them when regenerating again? |
Ah! Great question. I personally think this is out of scope for a project like this, since the goal is to translate protobuf to python. The validation is enforced by the underlying protobuf model since this is a 1:1 translation (mainly to enforce types and groups). If I had other business logic to validate, I would add methods dynamically in a |
@Gobot1234 Fixed all suggestions and added text in the README. Double checking that everything still works since I merged w/master. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty much ready to ship
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
@Gobot1234 All is good on my side; the merge w/master did not affect anything. If this ever need maintenance or issues pop-up, feel free to @ me and I'll be right there. |
Thanks for all your work on this |
Pydantic dataclasses are a drop-in replacement for builtin dataclasses
to offer a stronger type validation. This PR adds support for
generating pydantic dataclasses instead of the builtin variant.
This enable usage of protobuf classes in API endpoint (like FastAPI)
which supports parsing pydantic objects but not builtin dataclasses.