🐛 Fix handling of invalid intermittend values

We not only ignore None values on our subkey paths, we ignore
anything that isn't a dict we can further level down in.

Closes #4479
This commit is contained in:
Gina Häußge committed 2022-04-04 19:02:44 +02:00
1 parent 9faec6e982
commit f4b4f50cfd
2 files changed
+20 -2

No files matched your search

+16 -2
View File
@@ -577,16 +577,30 @@ class HierarchicalChainMap:
if d is None:
return {}
result = dict()
result = {}
for key, value in d.items():
if not key.startswith(prefix):
continue
subkeys = key[len(prefix) :].split(_CHAINMAP_SEP)
current = result
path = []
for subkey in subkeys[:-1]:
if subkey not in current or current[subkey] is None:
# we only need that for logging in case of data weirdness below
path.append(subkey)
# make sure the subkey is in the current dict, and that it is a dict
if subkey not in current:
current[subkey] = {}
elif not isinstance(current[subkey], dict):
logging.getLogger(__name__).warning(
f"There is a non-dict value on the path to {key} at {path!r}, ignoring."
)
current[subkey] = {}
# go down a level
current = current[subkey]
current[subkeys[-1]] = value
return result
+4
View File
@@ -791,6 +791,10 @@ class ChainmapTest(unittest.TestCase):
{_key("a"): None, _key("a", "b"): "b"},
{"a": {"b": "b"}},
),
(
{_key("a"): "", _key("a", "b"): "b"},
{"a": {"b": "b"}},
),
)
@ddt.unpack
def test_unflatten(self, value, expected):