deleting last key makes round_trip_dump create invalid yaml

Issue #160 new
Christopher Lambert
created an issue

python program:

if __name__ == '__main__':
    import ruamel.yaml as yaml

    print yaml.__version__

    with open("example.yaml", 'r') as yaml_file:
        data = yaml.round_trip_load(yaml_file)

    del data['root'][0]['some_key']

    with open("out.yaml", 'w') as yaml_file:
        yaml.round_trip_dump(data, yaml_file, indent=4)

    with open("out.yaml", 'r') as yaml_file:
        data = yaml.round_trip_load(yaml_file)


    # a comment
    - some_key: "value"

foo: 32
bar: 32

produces out.yaml:

    # a comment
-   {}

foo: 32 bar: 32

and the program fails with:

/usr/bin/python2.7 /mypath/
ordereddict([('root', [ordereddict([('some_key', 'value')])]), ('foo', 32), ('bar', 32)])
Traceback (most recent call last):
  File "/mypath/", line 16, in <module>
    data = yaml.round_trip_load(yaml_file)
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 685, in round_trip_load
    return load(stream, RoundTripLoader, version, preserve_quotes=preserve_quotes)
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 636, in load
    return loader._constructor.get_single_data()  # type: ignore
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 102, in get_single_data
    node = self.composer.get_single_node()
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 75, in get_single_node
    document = self.compose_document()
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 96, in compose_document
    node = self.compose_node(None, None)
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 132, in compose_node
    node = self.compose_mapping_node(anchor)
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 194, in compose_mapping_node
    item_value = self.compose_node(node, item_key)
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 106, in compose_node
    if self.parser.check_event(AliasEvent):
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 144, in check_event
    self.current_event = self.state()
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 577, in parse_block_mapping_value
    if self.scanner.check_token(ValueToken):
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 1620, in check_token
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 1662, in _gather_comments
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 273, in fetch_more_tokens
    return self.fetch_value()
  File "/usr/local/lib/python2.7/dist-packages/ruamel/yaml/", line 626, in fetch_value
ruamel.yaml.scanner.ScannerError: mapping values are not allowed here
  in "out.yaml", line 5, column 12

some interesting observations:

  • removing the comment makes it work

  • removing one of foo/bar makes it work

  • removing the newline produces - {} foo: 32

  • error happens with and without indent=4

Comments (2)

  1. Ruamel/Anthon van der Neut repo owner

    Thank you for the report. I have no idea yet why exactly this might be happening and it might take a bit before I can look at it.

    BTW if you want your original layout then indent=4 is not what you should use instead look at indent=6, block_seq_indent=4.

  2. Log in to comment