Menu

#508 Trailing space after `:` when unbreakable strings are wrapped

open
nobody
None
major
bug
2025-09-08
2024-02-13
Antony Saba
No

When a value is a single, unbreakable string, a trailing space is added after the : before the newline.

#!/usr/bin/env python3

import sys
from ruamel.yaml import YAML

yaml = YAML()

doc = "---\nvalue:\n  verylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalue"
value = yaml.load(doc)
yaml.dump(value, sys.stdout)

doc = "---\nvalue: verylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalue"
value = yaml.load(doc)
yaml.dump(value, sys.stdout)

Using cat to show the extra spaces for both inputs:

> ./test.py | cat -e
value: $
  verylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalue$
value: $
  verylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalueverylongstringvalue$

SourceForge's formatting is messing with long lines and leading spaces, but the expected output would be to match one of the input doc strings above, since either one passes yamllint.

Discussion

  • Hauke

    Hauke - 2024-02-23

    I ran into a similar issue over at https://github.com/iterative/dvc/issues/10257#issuecomment-1915933299

    A temporary fix is to set yaml.width to a larger number so that the line break is not happening, e.g.:

    from ruamel.yaml import YAML
    yaml = YAML()
    yaml.widht = 10000
    d = {'a':{'b':'veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongpathorfilename.txt'}}
    with open("file.yaml", "w") as f:
        yaml.dump(d, f)
    

    The wrapping came into affect with ruamel-yaml version 0.17.22. Previous versions did not do this wrapping.

    The issue might happen here with the addition of a whitespace before the linebreak is made.

     
    👍
    2
    • Johannes Postler

      I ran into another issue, where this seems to happen in flow sequences. In such a case, strings with quotation marks are wrapped into the next line and sometimes inserting a whitespace in the process.

      I have an example at hand, if needed. Thanks to @Hauke for the hint at width.

       

Log in to post a comment.

MongoDB Logo MongoDB