Skip to content

Conversation

@martincpt
Copy link
Contributor

This PR introduces built-in Pydantic support via a new GenericEncoder that is compatible with both Pydantic V1 and V2.

Key changes:

  • GenericEncoder is 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.

@martincpt
Copy link
Contributor Author

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 lint command in the Makefile.
  • Could not verify the Makefile's docker-test commands as the necessary Dockerfiles are missing from the repository.
  • When running the Makefile's format command, 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?


def _is_pydantic_model_type(typ: Type) -> bool:
return any(
base.__module__.startswith("pydantic") and base.__name__ == "BaseModel"
Copy link
Owner

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?

Copy link
Contributor Author

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!

Copy link
Owner

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?

Copy link
Contributor Author

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.

@Ananto30
Copy link
Owner

Ananto30 commented May 4, 2025

Nice and neat work! 🙌

@martincpt
Copy link
Contributor Author

Additional updates:

  • Extended the Encoder protocol with an is_allowed_type method
  • Both the original MsgspecEncoder and the new GenericEncoder implement the type_util.is_allowed_type function
  • type_util.verify_function_input_type and type_util.verify_function_return_type now 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
Copy link

codecov bot commented May 14, 2025

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 ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Ananto30
Copy link
Owner

Ananto30 commented Jun 2, 2025

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.

@martincpt
Copy link
Contributor Author

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.

@martincpt
Copy link
Contributor Author

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! 🙏

@martincpt
Copy link
Contributor Author

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:

  1. issubclass(typ, BaseModel) raises a TypeError, 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?

  1. 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?

@martincpt
Copy link
Contributor Author

Any thoughts @Ananto30?

@Ananto30
Copy link
Owner

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!

@Ananto30 Ananto30 merged commit adc8bcc into Ananto30:main Jul 24, 2025
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants