-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
Move json parsing for complex field to pydantic #16
Comments
@dmontagu helped me to have a version of
class SubValue(BaseSettings):
v4: str
class TopValue(BaseSettings):
v1: str
sub: SubValue
class Cfg(BaseSettings):
top: TopValue
env.set('top', '{"v1": "json-1", "sub": {"v5": "xx"}}')
env.set('top__sub__v5', '5') In
foo: str = Field(validation_alias=AliasChoices('foo', AliasPath('foo1', 'bar', 1))) |
Ye I see the problem. I don't think aliases will work. Options:
I think option two is best |
Another problem with the new approach. Consider the following code: class Settings(BaseSettings):
top: Dict[str, str]
env.set('top', '{"banana": "secret_value"}')
s = Settings(top={'apple': 'value'})
assert s.top == {'apple': 'value', 'banana': 'secret_value'} The above code will fail in assertion because I am going to reject the new approach because:
What do you think @samuelcolvin |
Agreed, sorry for the mistaken suggestion. |
But we do need a config flag for whether to parse JSON, and we do need to inspect the core_schema to see if a field is "complex". |
Why do we need the flag?
we still need to handle the |
There's an issue somewhere on pydantic (can't find it right now) where people completely legitimately want to parse things in their own way using validators, but the JSON parsing is getting in the way. Users need a way to disable that. But maybe it's sufficient to have the |
Yes, they can override the |
We decided to don't use this approach |
Currently, there is a logic for parsing env variable values by
json.loads
whenever a field is complex.We may can move the parsing to Pydantic by using some hook to automatically insert the JSON validator in the core schema if field type is
dict
,list
,set
, ...Here is the suggested code by @samuelcolvin:
The text was updated successfully, but these errors were encountered: