Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line with extra spaces causes style changes. Bug or Feature? #712

Closed
d3cryptofc opened this issue May 3, 2023 · 4 comments
Closed

Line with extra spaces causes style changes. Bug or Feature? #712

d3cryptofc opened this issue May 3, 2023 · 4 comments

Comments

@d3cryptofc
Copy link

d3cryptofc commented May 3, 2023

Versions
Python 3.11.3
PyYAML 6.0

Parameters
Default Style: |
Allow Unicode: True

Input (contains extra spaces on line 3)

description: |
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam non porta urna. Cras vel mi accumsan, 
    
  pulvinar urna vitae, rutrum quam.

Wrong output

"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam non\
  \ porta urna. Cras vel mi accumsan, \n  \npulvinar urna vitae, rutrum quam."

Expected output

"description": |-
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam non porta urna. Cras vel mi accumsan,

  pulvinar urna vitae, rutrum quam.

Code

with open('input.yml', 'r') as file:
    data = yaml.safe_load(file)

with open('output.yml', 'w') as file:
    print(yaml.safe_dump(data, file, allow_unicode=True, default_style='|'))

Solution

Solved it for me by removing extra spaces in the YAML file.

@tsyg
Copy link

tsyg commented Aug 22, 2023

UPD: removed comment - it turned out to be not yaml bug, but pprint

@nitzmahone
Copy link
Member

This appears to be working as-designed: https://github.com/yaml/pyyaml/blob/6.0.1/lib/yaml/emitter.py#L759-L763 - basically the embedded spaces followed by a break are explicitly disallowed in block scalars by the emitter. It is curious that the parsers preserve it properly in a block scalar though- maybe something that was tightened up in later versions of the spec but the emitter code didn't evolve accordingly? That'd probably be an @ingydotnet question (or someone else more involved in the spec minuatiae).

Meantime, I'm going to close the issue for now, since the emitter code does seem to be designed to explicitly prevent this case.

@nitzmahone nitzmahone closed this as not planned Won't fix, can't repro, duplicate, stale Aug 22, 2023
@perlpunk
Copy link
Member

perlpunk commented Aug 22, 2023

It's valid YAML. Just that it was decided in PyYAML to disallow this even when literal style is explicitly requested by the user, and there are different opinions on whether it should allow it or not.
There's an issue for that: #121

@nitzmahone
Copy link
Member

Thanks @perlpunk for the issue necromancy- I've added #121 to the 6.1 planning project. I'm willing to do a little work to try any get this behaving, as well as getting the full YAML test suite integrated (since we're going to want that for a bunch of the other things we're talking about anyway ;) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants