IndexError when using .insert() after calling 'merge' property
ruamel.yaml is a YAML 1.2 parser/emitter for Python
Brought to you by:
anthon
Bug appears when calling CommentedMap.insert() after calling the merge property. See below.
The bug is present in latest version but I could trace its appearance starting in 0.17.24
from ruamel.yaml import YAML
yaml = YAML()
yaml_str = """\
first_name: Art
"""
data = yaml.load(yaml_str)
_ = data.merge # causes the bug to appear (as the property sets ._yaml_merge)
data.insert(0, "some_key", "test") # <- raises
File ~/.pyenv/versions/3.8.6/envs/.../lib/python3.8/site-packages/ruamel/yaml/comments.py:802, in CommentedMap.insert(self, pos, key, value, comment)
800 del self[key]
801 keys = [k for k in self.keys() if k in self._ok]
--> 802 ma0 = getattr(self, merge_attrib, [[-1]])[0]
803 merge_pos = ma0[0]
804 if merge_pos >= 0:
IndexError: list index out of range
Thanks for reporting, This should be fixed in 0.17.27
0.17.27 has been released with the fix for this