-
Notifications
You must be signed in to change notification settings - Fork 521
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
Incorrect indentation with lists #234
Comments
@bcoca can you please check if this is in the Python emitter code or if it's libyaml (ie CDumper) (or both)? |
@ingydotnet Testing with |
Thanks @sivel. This will need to be fixed in both. Patches welcome :) |
It seems like the problem is occurring because |
@zdog234 i've tried myself the below and didn't make any difference
or
|
@ingydotnet @perlpunk any chance you can shed some light and i may be able to fire a PR ? |
Well, I wouldn't call that behaviour incorrect. Let's look at both examples:
The top level mapping has an indentation of zero ( The value of the first sequence item, the mapping I assume you would expect this instead?
|
I can't speak for @DanyC97 but in my opinion yes, that last snippet is what is expected when passing |
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: american:
- Boston Red Sox
- Detroit Tigers
- New York Yankees
national:
- New York Mets
- Chicago Cubs
- Atlanta Braves The question is whether tools like 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: mapping:
one: 1
two: 2
list:
- 1
- 2 Therefore, I think that it is more appropriate to render sequences in such a way for indentation of size 2: mapping:
one: 1
two: 2
list:
- 1
- 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. |
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. |
From my point of view that would be the best, because IMHO currently block sequences are simple not indented at all (at least when they are a value of a block mapping), independent of the |
Is there any progress on this? |
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. |
Just ran into this myself in some example config generation I am doing, and it makes some of my exports rather weird. The indented list structure does seem more common, and it would be nice if pyyaml supported it. Edit: I've now also temporarily solved it with https://stackoverflow.com/questions/25108581/python-yaml-dump-bad-indentation. |
Also running into this via ansible. It would be nice if the indentation were consistent. |
The workaround mentioned above: class Dumper(yaml.Dumper):
def increase_indent(self, flow=False, *args, **kwargs):
return super().increase_indent(flow=flow, indentless=False)
print(yaml.dump(data, Dumper=Dumper)) Gateways:
- 14
- 4
- 18 |
I just use prettier as a pre-commit hook and it takes care of making yaml look good. |
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. |
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 not correctly indenting lists and will break the indentation every time a new validation will be created. This patch adds this file to the yamllint ignore list and provides a new molecule.yaml sorted by job names as the validation_init role will generate in a follow up patch. Moreover, this patch enables the oslo_config_validator molecule job in the check/gate as it was not added in the initial patch[3]. [1] ansible/ansible#48865 [2] yaml/pyyaml#234 [3] https://review.opendev.org/c/openstack/tripleo-validations/+/789698 Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com> Change-Id: Iac63dc6f161a0e885110a246b68001522a6f62e3
* Update tripleo-validations from branch 'master' to 7c35cd1fb7d641c10b7575e1dd354aea4e182dd9 - Merge "Add zuul.d/molecule.yaml file back in the yamllint ignore list" - 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 not correctly indenting lists and will break the indentation every time a new validation will be created. This patch adds this file to the yamllint ignore list and provides a new molecule.yaml sorted by job names as the validation_init role will generate in a follow up patch. Moreover, this patch enables the oslo_config_validator molecule job in the check/gate as it was not added in the initial patch[3]. [1] ansible/ansible#48865 [2] yaml/pyyaml#234 [3] https://review.opendev.org/c/openstack/tripleo-validations/+/789698 Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com> Change-Id: Iac63dc6f161a0e885110a246b68001522a6f62e3
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 correctly, due to [1][2]. [1] ansible/ansible#48865 [2] yaml/pyyaml#234 Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com> Change-Id: Ia8f787631c55fabb3a097919971d17b93711d4b7
* Update tripleo-validations from branch 'master' to ea3931c0f65278aefc784a633de99d0f225414b5 - Merge "Sort jobs in molecule.yaml file when adding new role" - 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 correctly, due to [1][2]. [1] ansible/ansible#48865 [2] yaml/pyyaml#234 Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com> Change-Id: Ia8f787631c55fabb3a097919971d17b93711d4b7
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 not correctly indenting lists and will break the indentation every time a new validation will be created. This patch adds this file to the yamllint ignore list and provides a new molecule.yaml sorted by job names as the validation_init role will generate in a follow up patch. Moreover, this patch enables the oslo_config_validator molecule job in the check/gate as it was not added in the initial patch[3]. [1] ansible/ansible#48865 [2] yaml/pyyaml#234 [3] https://review.opendev.org/c/openstack/tripleo-validations/+/789698 Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com> Change-Id: Iac63dc6f161a0e885110a246b68001522a6f62e3 (cherry picked from commit 41dcda8)
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 correctly, due to [1][2]. [1] ansible/ansible#48865 [2] yaml/pyyaml#234 Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com> Change-Id: Ia8f787631c55fabb3a097919971d17b93711d4b7 (cherry picked from commit ae59abe)
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 not correctly indenting lists and will break the indentation every time a new validation will be created. This patch adds this file to the yamllint ignore list and provides a new molecule.yaml sorted by job names as the validation_init role will generate in a follow up patch. Moreover, this patch enables the oslo_config_validator molecule job in the check/gate as it was not added in the initial patch[3]. [1] ansible/ansible#48865 [2] yaml/pyyaml#234 [3] https://review.opendev.org/c/openstack/tripleo-validations/+/789698 Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com> Change-Id: Iac63dc6f161a0e885110a246b68001522a6f62e3 (cherry picked from commit 41dcda8) (cherry picked from commit f798c56)
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 correctly, due to [1][2]. [1] ansible/ansible#48865 [2] yaml/pyyaml#234 Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com> Change-Id: Ia8f787631c55fabb3a097919971d17b93711d4b7 (cherry picked from commit ae59abe) (cherry picked from commit cc29577)
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 not correctly indenting lists and will break the indentation every time a new validation will be created. This patch adds this file to the yamllint ignore list and provides a new molecule.yaml sorted by job names as the validation_init role will generate in a follow up patch. Moreover, this patch enables the oslo_config_validator molecule job in the check/gate as it was not added in the initial patch[3]. [1] ansible/ansible#48865 [2] yaml/pyyaml#234 [3] https://review.opendev.org/c/openstack/tripleo-validations/+/789698 Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com> Change-Id: Iac63dc6f161a0e885110a246b68001522a6f62e3 (cherry picked from commit 41dcda8) (cherry picked from commit f798c56) (cherry picked from commit 04c0871)
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 correctly, due to [1][2]. [1] ansible/ansible#48865 [2] yaml/pyyaml#234 Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com> Change-Id: Ia8f787631c55fabb3a097919971d17b93711d4b7 (cherry picked from commit ae59abe) (cherry picked from commit cc29577) (cherry picked from commit da80177)
another year 2022 coming, is there an easy way to resolve this issue? |
This worked for me. It is quite easy to implement also. |
See: yaml/pyyaml#234 (comment) At least in Fedora 36 / 37 increase_indent does not work with CDumper. python3-pyyaml-6.0-3.fc36.x86_64 python3-pyyaml-6.0-5.fc37.x86_64 Python 3.10.6 (main, Aug 2 2022, 00:00:00) [GCC 12.1.1 20220507 (Red Hat 12.1.1-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import yaml >>> >>> try: ... from yaml import CDumper as yaml_Dumper ... except ImportError: ... from yaml import Dumper as yaml_Dumper ... >>> class TestDumper(yaml_Dumper): ... def increase_indent(self, flow=False, indentless=False): ... return super(yaml_Dumper, self).increase_indent(flow, False) ... >>> yaml.dump({"a":[1]}, None, TestDumper) 'a:\n- 1\n' >>> >>> unformatted_yaml = yaml.dump({"a":[1]}, None, TestDumper) >>> if unformatted_yaml == 'a:\n- 1\n': ... from yaml import Dumper as yaml_Dumper ... >>> class TestDumper(yaml_Dumper): ... def increase_indent(self, flow=False, indentless=False): ... return super(yaml_Dumper, self).increase_indent(flow, False) ... >>> yaml.dump({"a":[1]}, None, TestDumper) 'a:\n - 1\n'
Sill not work, will this be fixed? my code def test_demo(self):
data = {
"name": "John",
"age": 30,
"city": "New York",
"haha": ["aaaa", "bbbb"]
}
# 设置 indent 和 default_flow_style 参数
output = yaml.dump(data, Dumper=Dumper, sort_keys=False,indent=2)
print(output) result is age: 30
city: New York
haha:
- aaaa
- bbbb what I expected is age: 30
city: New York
haha:
- aaaa
- bbbb |
Found a temp solution |
What in the words |
@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. |
This addresses yaml/pyyaml#234. PyYAML is currently ignoring the indent for lists when dumping. Since ansible-lint expects the indent, this is problematic.
I just discovered this issue, and I'm running into it. It is ridiculous that the team around pyyaml hasn't provided a fix for this since 2018 |
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.original issue ansible/ansible#48865
The text was updated successfully, but these errors were encountered: