pyhocon icon indicating copy to clipboard operation
pyhocon copied to clipboard

ConfigTree.merge_configs fails when one ConfigTree comes from file and another from dict

Open gnhernandez opened this issue 8 years ago • 3 comments

On recent versions of pyhocon the merge_configs methods make reference to the attribute "history" of the ConfigTree. This attribute is set when read from file, but is not set when CofigFactory.from_dict is called. This causes the merge_configs methods to fail when e.g. a file is read and some dictionary of overrides is to be merged on top of the file options.

gnhernandez avatar Nov 02 '17 06:11 gnhernandez

Also experienced this. The error is

AttributeError: 'ConfigTree' object has no attribute 'history'

tekumara avatar Mar 28 '18 07:03 tekumara

I'm also running into this. For me it's a little more subtle and appears to be dependent on whether the dict overrides values in some or all sub-trees of the file-based config.

tfenne avatar Apr 15 '18 14:04 tfenne

My temporary/hacky workaround for this is to parse the dictionary once using ConfigFactory.from_dict() to ensure any dot.separated keys are handled correctly, then dump it to json and re-parse with ConfigFactory.parse_string(). E.g.

c1 = ConfigFactory.parse_file("some.conf")
c2 = ConfigFactory.from_dict({"foo": 1, "bar.splat": 2})
c2 = ConfigFactory.parse_string(json.dumps(c2))

It's a little hacky, but seems to handle everything and results in a valid config.

tfenne avatar Apr 15 '18 15:04 tfenne