Add GenericEncoder and Pydantic support
This PR introduces built-in Pydantic support via a new GenericEncoder that is compatible with both Pydantic V1 and V2.
Key changes:
GenericEncoderis now the default encoder.- Pydantic has been added as an optional dependency and can be installed with:
pip install zeroapi[pydantic]
- Improved type annotations across the encoders for better clarity and type checking.
Additional updates:
- Added missing test using a Pydantic model.
- Increased coverage for
GenericEncoder. - Extended the codegen tool to support Pydantic
BaseModel, and updated the codegen tests accordingly.
Other notes:
- Verified the
lintcommand in the Makefile. - Could not verify the Makefile's
docker-testcommands as the necessary Dockerfiles are missing from the repository. - When running the Makefile's
formatcommand, it reformats files I didn’t modify — mostly adjusting ellipses (...). This might be due to a newer version of black. Would you like me to include these formatting changes in the PR as well?
Nice and neat work! :raised_hands:
Additional updates:
- Extended the
Encoderprotocol with anis_allowed_typemethod - Both the original
MsgspecEncoderand the newGenericEncoderimplement thetype_util.is_allowed_typefunction type_util.verify_function_input_typeandtype_util.verify_function_return_typenow accept the encoder instance to validate against- Tests have been updated accordingly
@Ananto30 Could you please review when you have time?
Also, I'm not sure why Codacy Static Code Analysis is flagging tests/requirements.txt. I only added pydantic, so it might be a false positive — could you take a quick look? Not sure how to ignore that one properly.
Thanks! 🙏
Codecov Report
Attention: Patch coverage is 93.75000% with 3 lines in your changes missing coverage. Please review.
Project coverage is 97.46%. Comparing base (
d19eaac) to head (e095b8e).
| Files with missing lines | Patch % | Lines |
|---|---|---|
| zero/encoder/generic.py | 84.21% | 3 Missing :warning: |
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
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
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 ImportError scenario, as I couldn’t rerun the top-level module imports reliably, even with mocking — and I’m short on time.
However, I was able to cover the pydantic v1 import and the related GenericEncoder logic, since it’s still available as a sub-package in v2. I’ve committed that test.
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:
issubclass(typ, BaseModel)raises aTypeError, which may be related to a known compatibility issue between Pydantic and Python 3.8. I’ve found a few GitHub and Stack Overflow discussions about similar errors, but it’s still unclear to me what’s causing it exactly. Interestingly, the tests pass correctly on all other Python versions. I also tried installing Python 3.8 and Pydantic locally but wasn’t able to reproduce the issue.
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?
- The second issue occurs when running make lint. It tries to import Pydantic, which is currently not installed by default. Since it’s an extra dependency, I added it to the install-lint command. Let me know if that helps.
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!