Skip to content
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/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def parse_config(
result: dict[str, Any] = {}
section_parts = [section.split(".") for section in config_parser.sections()]
# Phase 1: Build the skeleton of nested dicts from section names.
for parts in section_parts:
for parts in sorted(section_parts, key=len):
node = result
for part in parts[:-1]:
node = node.setdefault(part, {}) if part == "*" else node[part]
Expand Down
16 changes: 16 additions & 0 deletions tests/test_config_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

from configparser import InterpolationDepthError
from itertools import permutations

import pytest
from hypothesis import HealthCheck, given, settings
Expand All @@ -21,6 +22,21 @@
)


@pytest.mark.parametrize("order", list(permutations(range(4))))
def test_sections_can_precede_their_parents(order):
sections = [
"[training]\nseed = 42",
"[training.optimizer]\nrate = 0.01",
"[model]\nwidth = 64",
"[model.encoder]\ndepth = 2",
]
text = "\n\n".join(sections[i] for i in order)
assert Config().from_str(text) == {
"training": {"seed": 42, "optimizer": {"rate": 0.01}},
"model": {"width": 64, "encoder": {"depth": 2}},
}


@given(config_dicts)
def test_roundtrip(data):
"""Config.from_str(config.to_str()) should reproduce the original dict."""
Expand Down