I found a bug when dealing with YAML entries when there is a comment between the key and the array values. When I change the contents of the array to be an empty array, the library outputs an invalid YAML syntax, with the comment between the key name and an empty array ([]).
Code for reproduction (uploaded as file too just in case):
#!/usr/bin/env python3
from ruamel.yaml import YAML, __version__
yaml = YAML()
testfile = '/tmp/test.yml'
contents = """
root:
# A comment here
- key: value
"""
print(f"YAML version: {__version__}")
with open(testfile, 'w', encoding='UTF-8') as f:
f.write(contents)
with open(testfile, 'r', encoding='UTF-8') as f:
data = yaml.load(f)
print(f'Loaded data: {data}')
data['root'] = []
with open(testfile, 'w', encoding='UTF-8') as f:
yaml.dump(data, f)
with open(testfile, 'r', encoding='UTF-8') as f:
print("File contents after modification:")
print(f.read())
f.seek(0)
try:
new_data = yaml.load(f)
except Exception as e:
print(f"Re-read failed: {e}")
Output on my machine:
```
YAML version: 0.17.17
Loaded data: ordereddict([('root', [ordereddict([('key', 'value')])])])
File contents after modification:
root:
# A comment here
[]
Re-read failed: while scanning a simple key
in "/tmp/test.yml", line 3, column 1
could not find expected ':'
in "/tmp/test.yml", line 4, column 1
```
If I manage to get mercurial going on my machine I might give it a try this evening and see if I can fix it and send you a MR.
Seems to be related to https://sourceforge.net/p/ruamel-yaml/tickets/398/ and https://sourceforge.net/p/ruamel-yaml/tickets/397.
I got to the causing code but I'm not sure how to fix it properly without breaking things. To me it seems that
self.write_pre_comment(self.event)on emitter.py:437 should check if it is an empty sequence before writing it, but I am not sure what should be done with that comment and where to put it.There is no easy fix, otherwise I would already have pushed out a new version. I'll be looking at this when I have time, but comments between keys and value need work, as does handling flow style collections (which are forced upon us as YAML has no block style empty collections)
This should have been fixed in 0.17.18