Skip to content

proper enum schema for pydantic #651

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 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/betterproto/templates/template.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}":
Copy link
Collaborator

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?

Copy link
Author

@jbkoh jbkoh Dec 10, 2024

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.

Copy link
Author

@jbkoh jbkoh Dec 10, 2024

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

Copy link
Author

@jbkoh jbkoh Mar 18, 2025

Choose a reason for hiding this comment

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

@Gobot1234 I'm not sure if I understood your suggestion properly actually. At least, I changed to use lambda instead.
Another option is just to define this validator as a class method for class Enum by default.

Please elaborate your suggestion again and I'd be happy to update my PR. Thanks!

return cls(value)

# Return the schema for validation and serialization
return core_schema.chain_schema(
[
core_schema.int_schema(ge=0), # Validate as a string first
core_schema.no_info_plain_validator_function(validate), # Custom validation
]
)
{% endif %}

{% endfor %}
Expand Down
9 changes: 9 additions & 0 deletions tests/inputs/enum/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
Test,
)

from tests.output_betterproto_pydantic.enum import (
Test as TestPyd,
Choice as ChoicePyd,
)


def test_enum_set_and_get():
assert Test(choice=Choice.ZERO).choice == Choice.ZERO
Expand Down Expand Up @@ -112,3 +117,7 @@ def test_renamed_enum_members():
"MINUS",
"_0_PREFIXED",
}

def test_pydantic_enum_preserve_type():
test = TestPyd(choice=ChoicePyd.ZERO)
assert isinstance(test.choice, ChoicePyd)
Loading