New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sort keys in dicts in output yaml for 'config' command #1049
Conversation
Having different output each time due to random key order makes some tasks harder (such as CI scripts).
Sure! |
Having different output each time due to random key order makes some tasks harder (such as CI scripts).
Having different output each time due to random key order makes some tasks harder (such as CI scripts).
@ivan4th Please explain what you mean by "random key order". ESPHome uses OrderedDicts in config parsing practially everywhere, so the order of the keys should match that of how you entered them. This is also why the values should not be sorted, this way the keys are in the same order as before and it's easier to see when looking over the output. Plus in Python 3.6 you have have guaranteed insertion order for normal dicts, so even if OrderedDict is not used somewhere that should not be an issue. |
This reverts commit 64bd33a.
@OttoWinter The observed behaviour at the time of this PR was indeed this: each time I run esphome with the same input, I get different key order in the output. Now I suspect this could be related to this PyYAML bug, though, so as it turns out |
Ok you appear to be right: $ for i in {1..4}; do diff -u <(esphome tests/test$i.yaml config) <(esphome tests/test$i.yaml config); done
--- /proc/self/fd/12 2020-07-24 22:41:42.194354945 +0200
+++ /proc/self/fd/14 2020-07-24 22:41:42.194354945 +0200
@@ -27,10 +27,10 @@
blue: 0.0
white: 1.0
build_path: build/test1
- libraries: []
- arduino_version: espressif32@1.12.1
includes: []
+ libraries: []
platformio_options: {}
+ arduino_version: espressif32@1.12.1
# ... |
Actually, seems like the most recent dev commit 43d5e7a appears to still have this problem, so new PyYAML didn't help after all :( So either it is not completely fixed in PyYAML or there's another issue in esphome. I just tried reverting my commit and got the problem of varying output again. So, while sorting keys may not be the proper fix (if we want to keep user's key order), reverting the commit is not correct thing to do either (as random key order doesn't add any value), so we need to understand the cause of this problem. It's already late here, so I'll try to look into thi on weekend. Quick test with docker to make sure my env doesn't affect things:
|
Ok, I found the issues (neither directly related to PyYAML) and fixed them here:
Now the test script I wrote above works fine. |
@OttoWinter yes, this indeed works fine, thanks! 👍 |
Description:
Having different output each time due to random key order makes some
tasks harder (such as CI scripts). So, let's sort the keys to have same yaml output each time.
Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#
Checklist:
tests/
folder).If user exposed functionality or configuration variables are added/changed: