Missing comments from round-trip dumper between list items

Issue #163 new
Mike Kazantsev
created an issue
#!/usr/bin/env python3

import os, sys, io
import ruamel.yaml

yaml_str = '''
some-list:
# List comment
- {}
'''

yaml = ruamel.yaml.YAML(pure=True)
with io.StringIO(yaml_str) as src: data = yaml.load(src)
yaml.dump(data, sys.stdout)

output:

some-list:
- {}

expected output:

some-list:
# List comment
- {}

Workarounds:

  • Indenting list item fixes the issue (comment preserved).

Versions:

  • ruamel.yaml version: 0.15.34
  • python sys.version: 3.6.2 (default, Jul 20 2017, 03:52:27) \n[GCC 7.1.1 20170630]

Comments (3)

  1. Ruamel/Anthon van der Neut repo owner

    Thanks for the various reports. It might take a bit of time before I can look at these.

    I hope you know it is not necessary to use io.StringIO on input, you can directly pass the multiline string: yaml.load(yaml_str), which is slightly more efficient (assuming you already have the YAML document as string). I would use a backslash to prevent the first line from being empty:

    yaml_str = """\
    some-list:
    # list comment
    - {}"""
    
  2. Mike Kazantsev reporter

    Comment here is not indented same as the item below (somewhat similar to what you've ponted out in #164), but indenting it doesn't help, so guess it's a bug, unlike #164 or #162.

    Thanks for working on this awesome module, wanted something like this quite a few times, but never had the heart to implement it due to complexity of yaml.

  3. Ruamel/Anthon van der Neut repo owner

    There is definitely something wrong here, when I put two spaces before the - {} and roundtrip, the comment reappears but the end-of-document newline is gone. This has two do with not being able to cope properly with comments between key and value.

  4. Log in to comment