-
Notifications
You must be signed in to change notification settings - Fork 218
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
proper enum schema for pydantic #651
base: master
Are you sure you want to change the base?
proper enum schema for pydantic #651
Conversation
just added a test too. |
@Gobot1234 please review and let me know if it's in the right direction. |
@@ -17,7 +17,16 @@ class {{ enum.py_name }}(betterproto.Enum): | |||
def __get_pydantic_core_schema__(cls, _source_type, _handler): | |||
from pydantic_core import core_schema | |||
|
|||
return core_schema.int_schema(ge=0) | |||
def validate(value: int) -> "{{ enum.py_name }}": |
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.
Having a function for each of these adds a lot of bulk to the generated files, can this not go in the original definition for Enum and also be inlined into a lambda as this doesn't need type hints?
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.
I'm fine with the suggestion. Where do you recommend to declare the validate function? We can also define it as a method in betterproto library too.
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.
something like
def get_validate_betterproto_enum(cls):
def validate(cls, value):
return cls(value)
return validate
Summary
Checklist