Anthon
Anthon

Reputation: 76862

SIngle character y and Y get dumped as YAML 1.1 booleans

When I do:

from ruamel import yaml

seq = ["x", "y", "z", "Y", "true", True]
print(yaml.dump(seq, version=(1,1)))

it gives:

%YAML 1.1
--- [x, y, z, Y, 'true', true]

but I expected the y and Y to be quoted, because these get loaded back as booleans because this is YAML 1.1. Moreover this bug, indicates this problem is solved.

Why is this bug marked as closed, when it still shows this error even on version ruamel.yaml>=0.15.93?

Upvotes: 1

Views: 162

Answers (1)

Anthon
Anthon

Reputation: 76862

You are using the unsafe PyYAML compatibility function dump() (and besides you do so in an inefficient way). That function is outdated but emulates PyYAML's erroneous behaviour.

You should instead instantiating a YAML() instance and using its .dump() method.

import sys
import yaml as pyyaml
import ruamel.yaml

seq = ["x", "y", "z", "Y", "true", True]
print("PyYAML version:", pyyaml.__version__)
pyyaml.dump(seq, sys.stdout, default_flow_style=None, explicit_start=True, version=(1,1))
print()

yaml = ruamel.yaml.YAML(typ='safe')
yaml.version = (1,1)
yaml.default_flow_style=None
print("ruamel.yaml version:", ruamel.yaml.__version__)
yaml.dump(seq, sys.stdout)

which gives:

PyYAML version: 5.3.1
%YAML 1.1
--- [x, y, z, Y, 'true', true]

ruamel.yaml version: 0.16.10
%YAML 1.1
--- [x, 'y', z, 'Y', 'true', true]

Upvotes: 1

Related Questions