Menu

#426 Comments are not preserved

wont-fix
nobody
None
major
bug
2022-04-02
2022-04-02
Cerin
No

The documentation states that one of the primary features of the ruamel.yaml package is "roundtrip preservation of comments", and this is mostly why I use this package. However, this feature no longer works reliably, and comments are now partially stripped from all YAML files.

The following Bash script reproduces the environment and behavior:

virtualenv -p python3.9 yaml_test
. yaml_test/bin/activate
pip install ruamel.yaml==0.17.21
cat <<EOT >> test.yaml
# Some comment.
some_key: 123
# Some other comment.
some_other_key: 456
EOT
cat <<EOT >> test.py
import ruamel.yaml
with open('test.yaml') as fin:
    yaml_text = fin.read()
content = ruamel.yaml.round_trip_load(yaml_text)
with open('test2.yaml', 'w') as fout:
    ruamel.yaml.round_trip_dump(content, fout)
EOT

This code simply reads and then writes a YAML file, using ruamel.yaml's round trip read and write.

Expected behavior is that the outputted file is identical to the inputted file.

Actual behavior is that the outputted file has a comment removed.

Notice the second YAML comment "# Some other comment." is not present in the generated file.

In this example, the first comment is retained while the second is lost. However, on larger files in my production system, which comments are stripped seems almost nondeterministic. In some example, the first several comments are lost while dozens afterwards are retained.

The last known working version is 0.17.4. I encountered this bug while trying to upgrade to 0.17.21, and this error was caught in my regression testing.

Discussion

  • Anthon van der Neut

    This is not going to be fixed, as the function round_trip_loader as part of the old API has been replaced in 0.15 (released in 2017, see https://yaml.readthedocs.io/en/latest/api.html), it is now deprecated and will actually be removed in next (0.18.0) release.

    You should switch to using a YAML() instance:

    import sys
    import ruamel.yaml
    
    yaml_str = """\
    # Some comment.
    some_key: 123
    # Some other comment.
    """
    
    yaml = ruamel.yaml.YAML()  # the default is round-trip loading/dumping
    data = yaml.load(yaml_str)
    yaml.dump(data, sys.stdout)
    

    this gives (with 0.17.21):

    # Some comment.
    some_key: 123
    # Some other comment.
    
     
  • Anthon van der Neut

    • status: open --> wont-fix
     

Log in to post a comment.