Skip to content
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

Removed arbitrary_types_allowed=True from MultipleChoiceQuestion via custom Pydantic serializer/validator #178

Merged
merged 3 commits into from
Jan 21, 2025

Conversation

jamesbraza
Copy link
Collaborator

#177 added random.Random support, but also added arbitrary_types_allowed=True to MultipleChoiceQuestion. I didn't like that arbitrary_types_allowed, so I tapped into Pydantic v2's custom validator/serializer logic to allow serialization of random.Random.

@jamesbraza jamesbraza added the enhancement New feature or request label Jan 21, 2025
@jamesbraza jamesbraza requested review from sidnarayanan and a team January 21, 2025 18:47
@jamesbraza jamesbraza self-assigned this Jan 21, 2025
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jan 21, 2025
@@ -211,6 +212,37 @@ async def extract_answer(
return None


class RandomAnnotation:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Out of curiosity, why go this route instead of field_serializer/validator?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah good question, it's for reusability. I can easily import/reuse RandomAnnotation across many different BaseModel, whereas field_serializer/validator are class-specific and not reusable

@jamesbraza jamesbraza merged commit 8638373 into main Jan 21, 2025
6 checks passed
@jamesbraza jamesbraza deleted the allowing-random-pydantic branch January 21, 2025 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants