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

Fix nested fields not json parsed when they are complex #3971 #4235

Closed
wants to merge 4 commits into from

Conversation

rastaclaus
Copy link

@rastaclaus rastaclaus commented Jul 13, 2022

Change Summary

composite fields of nested objects are now json-parsed

Related issue number

fix pydantic/pydantic-settings#41

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@rastaclaus
Copy link
Author

All tests ok, please review, thanks (=

@rastaclaus rastaclaus changed the title Fix Nested fields not json parsed when they are complex #3971 Fix nested fields not json parsed when they are complex #3971 Jul 14, 2022
@hramezani
Copy link
Member

Thanks @rastaclaus for the patch 👍

I think we already have another fix in #4216 that seems more complete than yours.
Also, it seems your proposed solutions will fail by below test:

def test_nested_env_complex_values(env):
    class SubSubModel(BaseSettings):
        dvals: Dict

    class SubModel(BaseSettings):
        vals: List[str]
        sub_sub_model: SubSubModel

    class Cfg(BaseSettings):
        sub_model: SubModel

        class Config:
            env_prefix = 'cfg_'
            env_nested_delimiter = '__'

    env.set('cfg_sub_model__vals', '["one", "two"]')
    env.set('cfg_sub_model__sub_sub_model__dvals', '{"three": 4}')

    assert Cfg().dict() == {'sub_model': {'vals': ['one', 'two'], 'sub_sub_model': {'dvals': {'three': 4}}}}

    env.set('cfg_sub_model__vals', 'invalid')
    with pytest.raises(SettingsError, match='error parsing JSON for "cfg_sub_model__vals'):
        Cfg()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nested fields not json parsed when they are complex
4 participants