calliope-project / calliope

A multi-scale energy systems modelling framework
https://www.callio.pe
Apache License 2.0
277 stars 89 forks source link

AttrDict sorts keys on union #576

Closed brynpickering closed 4 months ago

brynpickering commented 4 months ago

What happened?

This is intended behaviour, but it is silent and probably not what we actually want it to do:

Say we have:

foo = AttrDict({"c": 1, "b": 2})
bar = AttrDict({"d": 10, "b": 2, "a": 20})
foo.union(bar, allow_override=True)

We get:

{'c': 1, 'b': 2, 'a': 20, 'd': 10}

I would expect the result to actually be:

{'c': 1, 'b': 2, 'd': 20, 'a': 10}

The reason the keys in bar are being sorted alphabetically is that in AttrDict.union keys_nested is being called, which sorts keys before returning them nested. bar then has its (sorted) keys iterated over to merge into foo, hence why the keys from foo are not sorted.

To achieve what I think would be more intuitive (and more useful when adding user-defined YAML math) behaviour, we should remove key sorting when calling keys_nested.

Which operating systems have you used?

Version

v0.7.0.dev3

Relevant log output

No response