pyyaml: Incorrect indentation with lists
When using indentation this seems to be applied to the value of the list instead of to the list itself, as you can see below indent=4
is applied after the leading -
and not to the list itself.
>>> print(yaml.dump(data['vars']['yaml'], indent=4, allow_unicode=True, default_flow_style=False))
list_of_dict_attr:
- attr1: value1
attr2: value2
attr3:
- item1
- item2
single_attr: value1
>>> print(yaml.dump(data['vars']['yaml'], indent=2, allow_unicode=True, default_flow_style=False))
list_of_dict_attr:
- attr1: value1
attr2: value2
attr3:
- item1
- item2
single_attr: value1
original issue https://github.com/ansible/ansible/issues/48865
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 38
- Comments: 25 (4 by maintainers)
Links to this issue
Commits related to this issue
- Add zuul.d/molecule.yaml file back in the yamllint ignore list The validation_init role auto generates the molecule.yaml file when adding a new role. But, due to [1][2], the to_nice_yaml filter is no... — committed to openstack-archive/tripleo-validations by strider 3 years ago
- Update git submodules * Update tripleo-validations from branch 'master' to 7c35cd1fb7d641c10b7575e1dd354aea4e182dd9 - Merge "Add zuul.d/molecule.yaml file back in the yamllint ignore list" - Ad... — committed to openstack/openstack by deleted user 3 years ago
- Sort jobs in molecule.yaml file when adding new role This patch adds a mechanism to keep the job names in zuul.d/molecule.yaml sorted. Therefore, the to_nice_yaml filter is not indenting lists correc... — committed to openstack-archive/tripleo-validations by strider 3 years ago
- Update git submodules * Update tripleo-validations from branch 'master' to ea3931c0f65278aefc784a633de99d0f225414b5 - Merge "Sort jobs in molecule.yaml file when adding new role" - Sort jobs in... — committed to openstack/openstack by deleted user 3 years ago
- Add zuul.d/molecule.yaml file back in the yamllint ignore list The validation_init role auto generates the molecule.yaml file when adding a new role. But, due to [1][2], the to_nice_yaml filter is no... — committed to openstack-archive/tripleo-validations by strider 3 years ago
- Sort jobs in molecule.yaml file when adding new role This patch adds a mechanism to keep the job names in zuul.d/molecule.yaml sorted. Therefore, the to_nice_yaml filter is not indenting lists correc... — committed to openstack-archive/tripleo-validations by strider 3 years ago
- Add zuul.d/molecule.yaml file back in the yamllint ignore list The validation_init role auto generates the molecule.yaml file when adding a new role. But, due to [1][2], the to_nice_yaml filter is no... — committed to openstack-archive/tripleo-validations by strider 3 years ago
- Sort jobs in molecule.yaml file when adding new role This patch adds a mechanism to keep the job names in zuul.d/molecule.yaml sorted. Therefore, the to_nice_yaml filter is not indenting lists correc... — committed to openstack-archive/tripleo-validations by strider 3 years ago
- Add zuul.d/molecule.yaml file back in the yamllint ignore list The validation_init role auto generates the molecule.yaml file when adding a new role. But, due to [1][2], the to_nice_yaml filter is no... — committed to openstack-archive/tripleo-validations by strider 3 years ago
- Sort jobs in molecule.yaml file when adding new role This patch adds a mechanism to keep the job names in zuul.d/molecule.yaml sorted. Therefore, the to_nice_yaml filter is not indenting lists correc... — committed to openstack-archive/tripleo-validations by strider 3 years ago
- kludge: ssg/yaml: f36 regression with CDumper increase_indent See: https://github.com/yaml/pyyaml/issues/234#issuecomment-786026671 At least in Fedora 36 / 37 increase_indent does not work with CDum... — committed to maage/scap-security-guide by maage 2 years ago
The workaround mentioned above:
From my point of view, the most widely-accepted indentation style for sequences is the one used multiple times in the official YAML specification. For instance, in section 2.1, example 2.3 looks like this:
The question is whether tools like
pyyaml
should render sequences in such a way for indentation of size 4 or for indentation of size 2.I would argue that it seems incorrect to render sequences in such a way for indentation of size 4, because other items would visually appear to be indented more:
Therefore, I think that it is more appropriate to render sequences in such a way for indentation of size 2:
That being said, someone may prefer to not indent sequence items to a level that is visually similar to the indentation level of the other items. That is a fair requirement, but in order to fully support it, there would have to be a separate configuration option for indentation size of sequences.
Well, I wouldn’t call that behaviour incorrect. I guess it’s a matter of taste, and I can find arguments proving that it’s consistent. What I’m also missing in this issue is the expected correct behaviour.
Let’s look at both examples:
The top level mapping has an indentation of zero (
0 * spaces
). The value forlist_of_dict_attr
, the sequence, also has an indentation of zero, because PyYAML chooses zero-indented sequences always. That’s why the dashes have no indentation in both cases. If it chooses zero-indentation, it simply does not depend on the number of spaces you configured.The value of the first sequence item, the mapping
attr1: ...
, has an indentation of1 * spaces
(respectively 4 or 2). The sequence underattr3
is zero-indented again, so1 * spaces
. The items of this sequence are on the same line, so they don’t get any indentation.I assume you would expect this instead?
another year 2022 coming, is there an easy way to resolve this issue?
@pkit I know you’re frustrated about the slow progress on this issue - many of us are - but please do not take your frustration out on your fellow commenters and contributors. By doing so you reduce trust and diminish the quality of all open source projects related to this one.
Also running into this via ansible. It would be nice if the indentation were consistent.
I agree with @pbasista about what the output should look like, that’ll be the same behavior that yamllint using, and maybe a separate configuration option would be a solution for both people want/like it or not.
I’m currently facing yaml file generated by pyyaml not being accepted by yamllint because of the indent of the lists.
@Acidherr
What in the words
indent=4
is so hard to understand? No it doesn’t work withindent=4
, in 2023.Unfortunately, it does not works with CDumper. And when working with a lots of yaml or with big yaml, I’d rather have no indentation than having a “good looking” one but much much slower generator.
I tried using prettier, but it made changes that both I and YamlLint disagreed with.
It seems the spec varies the output. The Preview section shows sequences indented from the key.
However the Failsafe Schema is indeed what pyyaml is doing:
Personally I prefer the indented format and it would be nice if pyyaml supported it as an option but the code isn’t doing anything wrong without the indents even if yamllint disagrees.
Is there any progress on this? Right now the workaround here is working for me https://stackoverflow.com/questions/25108581/python-yaml-dump-bad-indentation