pyyaml icon indicating copy to clipboard operation
pyyaml copied to clipboard

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

Open daniel-o-jones opened this issue 6 years ago • 2 comments

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?

daniel-o-jones avatar Jan 28 '20 02:01 daniel-o-jones

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.

perlpunk avatar Feb 01 '20 16:02 perlpunk

I digged a bit into this issue and found that it's this block which causes the problem in print(yaml.dump(True, explicit_start =False, explicit_end=False)) case.

yaml/emitter.py:

        elif isinstance(self.event, StreamEndEvent):
            if self.open_ended:
                self.write_indicator('...', True)
                self.write_indent()
            self.write_stream_end()
            self.state = self.expect_nothing

It seems that DocumentEndEvent is correctly handled (it contains an explicit attribute) while StreamEndEvent is not, It simply doesn't know about the user's request of explicit_end=True. I wonder if adding the explicit attribute to StreamEndEvent would be "good style":

aldpuzz avatar Sep 02 '25 14:09 aldpuzz