-
-
Notifications
You must be signed in to change notification settings - Fork 39
Add GenericEncoder and Pydantic support #63
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
|
Additional updates:
Other notes:
|
|
|
||
| def _is_pydantic_model_type(typ: Type) -> bool: | ||
| return any( | ||
| base.__module__.startswith("pydantic") and base.__name__ == "BaseModel" |
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.
Pydantic also has a GenericModel do we need to consider that?
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.
You're right—Pydantic does have GenericModel, but that's only part of Pydantic v1. Fortunately, GenericModel also inherits from BaseModel, so any logic that targets BaseModel will still work seamlessly for GenericModel instances too.
On a related note, I wanted to raise a question about the is_allowed_type check. In my experience, this utility has become a bottleneck for extending zero with support for new types. While the encoder side is quite flexible and easy to extend, is_allowed_type feels a bit restrictive—it requires explicitly adding each new supported type, which slows down development.
I'd suggest either removing this check altogether or generalizing it—for example, by checking isinstance(obj, type)—so it can handle any class-based types, including dataclasses, msgspec, and Pydantic models, without special casing.
Curious to hear your thoughts on this!
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.
Good point indeed! I wanted to boot zero up only when rpc functions are properly formatted, to eliminate parsing/marshalling errors. If we totally remove is_allowed_type then there's one more layer of debugging people might need to do when zero server is running properly. We can probably add this in the Encoder Protocol, wdyt? If someone wants to use a new Encoder, they can extend what we offer, but then they are responsible for supporting the types, otherwise we affect default people with the burden. wdyt?
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.
That's an excellent idea! I'll take a look at it this weekend.
|
Nice and neat work! 🙌 |
|
Additional updates:
@Ananto30 Could you please review when you have time? Also, I'm not sure why Codacy Static Code Analysis is flagging Thanks! 🙏 |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #63 +/- ##
==========================================
- Coverage 97.72% 97.46% -0.27%
==========================================
Files 24 25 +1
Lines 836 868 +32
==========================================
+ Hits 817 846 +29
- Misses 19 22 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Hi @martincpt , very few lines are missing in code coverage. I can merge without them if you want. Then will update and make a release. |
|
Hi @Ananto30, I covered codegen.py with my latest commit. I've also found solution to cover Pydantic V1 import but it does not tests the code with V1 actually. I'm currently a little busy. Please give me some time and I'll get back at you. |
|
Hi @Ananto30, sorry for keeping you waiting. Unfortunately, I wasn’t able to figure out a proper way to test the However, I was able to cover the pydantic v1 import and the related If you’re satisfied with this solution, please go ahead and merge the PR and create a new release. Thanks! 🙏 |
|
Hi @Ananto30, I’d like to get this PR finished — could we discuss how to move forward? There are at least two issues I’ve noticed:
Would you consider dropping support for Python 3.8, given that it’s officially discontinued? See: https://devguide.python.org/versions/ What are your thoughts?
How do you suggest we proceed to wrap this up? |
|
Any thoughts @Ananto30? |
|
Sorry @martincpt I was caught up with many things. Let me merge this and test. I will update the tests if needed. Thanks for the good work! |
This PR introduces built-in Pydantic support via a new
GenericEncoderthat is compatible with both Pydantic V1 and V2.Key changes:
GenericEncoderis now the default encoder.