Create ConfDict using json-mode model dump#53
Merged
jrapin merged 3 commits intofacebookresearch:mainfrom Mar 31, 2025
Merged
Conversation
Contributor
|
Would type casting work? e.g. from pydantic import BaseModel, field_validator
from datetime import timedelta
class MyModel(BaseModel):
duration: str
@field_validator("duration", mode="before")
@classmethod
def validate_duration(cls, value):
if isinstance(value, timedelta):
return str(value)
raise ValueError("duration must be a timedelta")
MyModel(duration=timedelta(days=1, hours=2, minutes=30)).duration |
Contributor
Author
|
@kingjr this type casting would work but it would mean re-implementing many functionalities already present in pydantic. For example, this is quite handy: import pydantic
import math
import datetime
class MyModel(pydantic.BaseModel):
x: pydantic.ImportString
y: datetime.timedelta
# Call with strings:
MyModel(x="math.sqrt", y="PT1M")
# MyModel(x=<built-in function sqrt>, y=datetime.timedelta(seconds=60))
# Or with the actual objects:
MyModel(x=math.sqrt, y=datetime.timedelta(minutes=1))
# MyModel(x=<built-in function sqrt>, y=datetime.timedelta(seconds=60))
# Converted back to strings to be JSON-compatible:
MyModel(x=math.sqrt, y=datetime.timedelta(minutes=1)).model_dump(mode='json’)
# {'x': 'math.sqrt', 'y': 'PT1M'}See all the types natively supported by pydantic for such casting to json-compatible formats: |
Contributor
|
Hi @PierreGtch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi @jrapin @kingjr,
Super cool library :D
I was playing a bit, but my model had some non-simple types (like
date time.timedeltaorpydantic.ImportString) that are not compatible withexec.ConfDict' sto_yamlandto_uidmethods.See, before fix:
This PR sets
model_dump(..., mode="json"), which uses pydantic's logic to convert many types to JSON-compatible ones.All the current tests pass. Could this change cause issues at non-tested places?