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

Support casting config values as dict with pydantic (#54) #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion confection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ def _update_from_parsed(
filled[key] = value
if key not in final:
final[key] = value
if isinstance(value, dict):
if isinstance(value, dict) and isinstance(final[key], dict):
filled[key], final[key] = cls._update_from_parsed(
value, filled[key], final[key]
)
Expand Down
28 changes: 28 additions & 0 deletions confection/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1431,3 +1431,31 @@ def test_parse_strings_interpretable_as_ints():
)
assert cfg["a"]["foo"] == [3, "003", "y"]
assert cfg["b"]["bar"] == 3


def test_dict_casting():
class CastStrAsDict:
@classmethod
def validate(cls, value):
if isinstance(value, str):
return {value: True}
return value

@classmethod
def __get_validators__(cls):
yield cls.validate

class SectionSchema(BaseModel):
key: CastStrAsDict

class MainSchema(BaseModel):
section: SectionSchema

cfg = Config().from_str(
"""\
[section]
key = "ok"
"""
)

assert my_registry.fill(cfg, schema=MainSchema) == {"section": {"key": "ok"}}