question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect indentation with lists

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:32
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

36reactions
pbasistacommented, Jun 28, 2019

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 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:

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.

32reactions
andreifcommented, Jan 23, 2021

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
Read more comments on GitHub >

github_iconTop Results From Across the Web

A bulleted list or a numbered list becomes misaligned ...
This problem occurs because Word 2007 does not automatically create a tab as part of a hanging indent for a bullet in a...
Read more >
The Indent Numbering Is Wrong on My Outline in Word
Word provides tools for fixing your lists if your numbers are out of order, the wrong combination of symbols are used or the...
Read more >
Consistent list indentation - CSS: Cascading Style Sheets | MDN
One of the most common style changes made to lists is a change in the indentation distance—that is, how far the list items...
Read more >
How To Avoid Indentation Error In Python
While coding you are using both the tab as well as space. · While programming you have placed an indentation in the wrong...
Read more >
Python indentation error when creating a list
It's look like you have tabs with spaces mix in your code. Symple copy-paste of your code from your question works correctly.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found