Fix from_dict() silently truncating dict keys that contain dots#349
Open
gaoflow wants to merge 1 commit into
Open
Fix from_dict() silently truncating dict keys that contain dots#349gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
ConfigTree.put() interprets dots in keys as HOCON path separators, so
from_dict({"a.b": v}) stored the value under the nested path a→b instead
of under the literal key "a.b", causing the dot-suffix to be silently dropped.
Two-part fix:
* config_parser.py: from_dict() now calls _put([key], …) with a
single-element path, preserving the Python dict key verbatim.
* config_tree.py: ConfigTree.get() first attempts an exact (OrderedDict)
lookup before falling back to dotted-path traversal, so literal dotted
keys stored by from_dict() are accessible via the normal API.
All 286 existing tests still pass; a new regression test covers the case
reported in issue chimpler#335.
Fixes chimpler#335
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ConfigFactory.from_dict()callsConfigTree.put(key, value)for each item in the input dictionary.put()passes the key throughConfigTree.parse_key(), which splits on HOCON path-separator characters — including.— treating them as a path rather than a literal key.This means a Python dict key like
"myThing_r0.1"is stored as a nested pathmyThing_r0 → 1, silently dropping everything after the last dot:Reported in issue #335.
Fix
Two minimal changes:
pyhocon/config_parser.py—from_dict()now callsres._put([key], …)instead ofres.put(key, …). Passing the key as a single-element path list bypassesparse_key()entirely, so the Python dict key is stored verbatim.pyhocon/config_tree.py—ConfigTree.get()now first attempts an exactOrderedDictlookup before falling back to dotted-path traversal. This makes literal dotted keys stored byfrom_dict()accessible through the normal API (cfg["myThing_r0.1"],.items(), etc.) without breaking existing HOCON path traversal.Testing
All 286 existing tests continue to pass. A new regression test (
test_from_dict_preserves_dotted_keys) covers the reported scenario.This pull request was prepared with the assistance of AI, under my direction and review.