simplistix / configurator

A Python library for handling configuration files.
https://configurator.readthedocs.io
MIT License
39 stars 6 forks source link

Assignments should strip off any ConfigNode wrappers. #15

Open wimglenn opened 1 year ago

wimglenn commented 1 year ago

I noticed that Config.__eq__ isn't implemented so we just get object.__eq__ comparison (identity based)

>>> from configurator import Config
>>> c1 = Config({"k": "v"})
>>> c2 = Config({"k": "v"})
>>> c1 == c2
False

Would it be possible to get a content-based equality comparison? In the simple example above we could just use c1.data == c2.data, but my real use-case has a nested configuration so comparing data attributes doesn't work either: the nested values are instances of ConfigNode, which also has identity-based comparison.

My use-case is in testing comparisons, where we want to assert some configs from multiple sources have been parsed and layered the way we expected, without having to recursively extract all the content from the Config instance. i.e., just compare with an "expected" Config or ConfigNode instance directly.

wimglenn commented 1 year ago

Nested containers remain an ConfigNode instances after mutation, reproducer:

>>> cfg = Config({"a": {"k1": [], "k2": {}}})
>>> cfg.a = dict(sorted(cfg.a.items(), reverse=True))
>>> cfg.data
{'a': {'k2': configurator.node.ConfigNode({}), 'k1': configurator.node.ConfigNode([])}}
cjw296 commented 1 year ago

Yeah, I feel like this is a tough one. Like we talked about: configurator should arguably strip off the ConfigNode wrapper at assignment time, but there will likely be cases where this doesn't work (assigning a dict mapping to ConfigNodes, etc).

I feel like the answer might want to be "you should be using the mapping/merging APIs" but want to leave this open for the next time I have a proper chance to play with this project...