question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why does pyyaml disallow trailing spaces in block scalars?

See original GitHub issue

We do not permit trailing spaces for block scalars.

Block style (|, <) is overruled with quoted style (") if the string contains trailing spaces. I don’t see any reason for this, and the fact that I can’t force a certain style (even it means losing information) has caused me a lot of grief.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:10
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
oulenzcommented, Apr 3, 2018

Thank you @perlpunk for these examples, they perfectly illustrate what I mean, and thank you both for looking into this.

My use case was that I had a large number of dictionaries that I wanted to store as yaml records, in | block style. Some of these happened to have trailing spaces and were output wrongly, as a double quoted scalar. This was frustrating because

  1. I had to reverse-engineer why this was happening, especially since I couldn’t find any reason in the yaml spec why my strings shouldn’t be representable in | block style. This really boggles the mind: pyyaml is needlessly deviating from the yaml spec. So this is a bug, that should be fixed.
  2. I was explicitly telling the emitter that I wanted | block style. The way every other Python function that I know of works is that the option I set is obeyed, possibly leading to warnings and/or errors, and that the consequences (like losing information in edge cases) are for me to deal with. This is programming, I should be able to control things. For pyyaml to override my option due to style considerations really is not acceptable.

My ‘solution’ was to strip trailing spaces before emitting so I would get | block style across the board, so ironically, this led me to lose information I wouldn’t have lost otherwise.

4reactions
perlpunkcommented, Apr 2, 2018

@ingydotnet , I was trying to give examples, to every reader of this issue, where I can understand the wish for block style to be preserved. I was not writing this to imply that you don’t know how it works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I control what scalar form PyYAML uses for my data?
It's worth noting that pyyaml disallows trailing spaces in block scalars and will force content into double-quoted format.
Read more >
YAML Multiline Strings
Block scalars have more control over how they are interpreted, whereas flow scalars ... indicator if the first line of the block starts...
Read more >
YAML Superpowers, part 2: Multiline Strings
In a multi-line block, YAML first determines the indentation level of the whole block: ... The leading spaces corresponding to (implicit or ...
Read more >
Details — Python YAML package documentation
PyYAML (and older versions of ruamel.yaml) gives you non-indented scalars (when ... that can be used to push the dash inwards, within the...
Read more >
YAML: The Missing Battery in Python
The leading whitespace on each line defines the scope of a block, ... You can define a YAML scalar similarly to a corresponding...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found