8000 Add GenericEncoder and Pydantic support by martincpt · Pull Request #63 · Ananto30/zero · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

martincpt
Copy link

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


class GenericEncoder(MsgspecEncoder):
def encode(self, data: Any) -> bytes:
if PYDANTIC_AVAILABLE and isinstance(data, BaseModel):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here

@Ananto30
Copy link
Owner
Ananto30 commented May 4, 2025

Nice and neat work! 🙌

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
0