Overriding nested keys
Clement-Hue opened this issue · 5 comments
Hi, I encountered an expected natural behavior,
When I wanted to override nested keys for instance
config["global"]["name"].set( "new_name")
the object "global" load from yaml file is emptied, and the only key which remains is "name" in my example, so I'm forced to do something like this
config["global"].set(**config["global"].get(), "name": "new_name")
For me this was counterintuitive, I spent time to realize that the overriding of the nested key was actually removing the other nested keys. But maybe I missed something ?
Hmm; to confirm, are you saying that config["global"].get()
returns {"name": "new_name"}
instead of a larger dict with more keys? That's a little odd, I admit—I'm not 100% sure whether it should work that way. Clearly it should be if you do config["global"].set(...)
, but using set
on the nested view should arguably do what you expect.
However, I think it should work to get all the merged values if you iterate over the collection. Give this a try:
for k, v in config["global"].items():
config["global"].get()
return all the keys of "global" this work as expected just if global has multiple keys and I override one like above config["global"]["name"].set( "new_name")
then "global" would end up with only one key, the one that has been overriden ("name" is this case). Which means that instead of just overriding the nested key, it's actually overriding the entire object.
To be more clear of what I said, yaml file:
nested:
key:
subkey1: "val1"
subkey2: "val2"
config_test["nested"]["key"]["subkey1"].set("override")
assert config_test["nested"]["key"]["subkey1"].get() == "override"
assert config_test["nested"]["key"].get() == {"subkey1": "override"}
The second assert should be {"subkey1": "override", "subkey2": "val2"}
Indeed, I do think this is the way it works (because the .get()
call looks for the first available source for the given view). Did you have a chance to try the iteration-based approach I mentioned?
No, I ended up doing my own sauce, this can be closed if it's the intended use.
Thanks for your time