Trailing space after `:` when unbreakable strings are wrapped
ruamel.yaml is a YAML 1.2 parser/emitter for Python
Brought to you by:
anthon
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.
I ran into a similar issue over at https://github.com/iterative/dvc/issues/10257#issuecomment-1915933299
A temporary fix is to set
yaml.widthto a larger number so that the line break is not happening, e.g.: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.
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.