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

Issue with explicit_end set to False having no impact on small dump #379

Open
daniel-o-jones opened this issue Jan 28, 2020 · 1 comment

Comments

@daniel-o-jones
Copy link

The following code leads to unexpected results
import yaml

print(yaml.dump(False, explicit_end=False))
print(yaml.dump(False, explicit_end=True))

Changing explicit_end results in no difference in output, with both printing three full stops and a new line after False.

print(yaml.dump({'key': False}, explicit_end=False))
print(yaml.dump({'key': False}, explicit_end=True))

Here changing explicit_end does result in a difference in output, with the former not printing the three full stops and newline.

Is this the desired behaviour?

@perlpunk
Copy link
Member

perlpunk commented Feb 1, 2020

The reason for that is, that a YAML document with only a plain scalar is considered "open-ended", and therefor needs an explicit document-end. So in this case the option is ignored.

This is important because of the YAML 1.1 specification, which allows concatenating documents
with a directive without the ...:

%YAML 1.1
--- "foo"
%YAML 1.1
--- "bar"

but for plain scalars the ... is needed:

%YAML 1.1
--- foo
...
%YAML 1.1
--- bar

YAML 1.2 does not allow the first example, and I would actually like to change the PyYAML behaviour to YAML 1.2, so that ignoring the option would not be necessary anymore. But that needs thorough testing.

jcfr added a commit to NA-MIC/ProjectWeek that referenced this issue May 24, 2023
The purpose of this commit are:

* revisits the generation of the project README to ensure the YAML
  values associated with the front-matter section are properly escaped.

* speed-up GitHub workflow by avoiding having to download the docker image
  associated with the GitHub Action "cuchi/jinja2-action"

* streamline debugging by outputting the content of "template-data.json"

* remove need for generating the "template-data.json" file in the current
  directory. Since no docker image is used for rendering the template, it
  can be used from the runner temp directory.


It is a follow-up of 66e3d34 (BUG: Remove ':' in project title)

To ensure the values are escaped, the jinja2 package is directly used. This
provides more flexibility as it allows to leverage the "to_yaml" and
"regex_replace" filters provided by the "jinja2-ansible-filters" package.

Note that since to_yaml is originally intended to output a complete
document, we need to make sure to strip "\n" or "\n...\n" from the
text as these do not make ensure in the context of a single YAML value.

For references related to the "...", see these links:
* https://stackoverflow.com/questions/56950391/yaml-end-always-dumped-even-if-yaml-explicit-end-false
* https://stackoverflow.com/questions/66542271/what-does-three-dots-in-a-yaml-yml-mean/66542407#66542407
* yaml/pyyaml#379
* ansible/ansible#17095
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

2 participants