pyyaml
pyyaml copied to clipboard
Customizing the threshold of check_simple_key().
Currently the threshold is hard coded as 128, and there is no way to override it.
Using a complex key on a string creates a file that sometimes uses sometimes complex key, sometimes uses a simple key on the same field of a list of mappings. This misalignment compromises the readability of the generated yaml file.
There is a stackoverflow ticket describing the issue: https://stackoverflow.com/questions/31197268
the culprit might be at that line: https://github.com/yaml/pyyaml/blob/48838a3c768e3d1bcab44197d800145cfd0719d6/lib/yaml/emitter.py#L452 (although it shows 128, not 122)
to test:
import yaml
doc={"a"*122: 1, "b"*123: 1} # 1st key is ok, 2nd is prefixed with ?
print(yaml.safe_dump(doc, width=10000))