7

I'm trying to debug an issue, and it boils down to...

>>> import yaml as pyyaml
>>> import ruamel.yaml as ruamel
>>> ruamel.load("x: yes") == ruamel.load("x: true")
False
>>> pyyaml.safe_load("x: yes") == pyyaml.safe_load("x: true")
True

There are rumors on the internet about "yes" and "no" being reserved words that are also synonyms for true and false respectively.

But there is only passing mention in the 1.1 spec but no elaboration, and the string "yes" doesn't appear in the 1.2 spec at all.

In fact looking through every draft of the spec it only appears in a legitimate way in any kind of legitimate way in https://yaml.org/spec/history/2002-09-01.html and is removed in the revision after.

I suspect I've answered my own question in the course of writing it, but... is this business about "yes/no" just nonsense that made its way into implementations (my editor is even highlighting "yes/no" as special), or am I misunderstanding or missing part of the spec?

jberryman
  • 16,334
  • 5
  • 42
  • 83

3 Answers3

6

Interpreting yes/no as true/false in yaml spec 1.1 was intentional and by design and is documented. However in yaml spec 1.2, interpreting yes/no as true/false was dropped.

From the PyYAML Documentation,

PyYAML supports the YAML 1.1 standard, ruamel.yaml supports YAML 1.2 as released in 2009.

YAML 1.2 dropped support for several features unquoted Yes, No, On, Off

Utku Özdemir
  • 7,390
  • 2
  • 52
  • 49
Prem Anand
  • 2,469
  • 16
  • 16
  • The only mention seems to be "For example, the boolean “true” might also be written as “yes”". This doesn't look intentional at all to me, unless I'm misunderstanding. – jberryman Aug 27 '19 at 22:20
  • ...and "off" does not appear. – jberryman Aug 27 '19 at 22:24
  • 1
    @jberryman Data types are not exhaustively discussed in the documentation spec itself. Instead, the spec refers to the schema documentation for the various tag formats. The one for the core `!!bool` tag is here: https://yaml.org/type/bool.html. It is, admittedly, not trivial to find. The impenetrable spec is in fact one of the core criticisms of YAML. – Konrad Rudolph Aug 27 '19 at 22:54
  • @KonradRudolph thanks! yes it looks like https://yaml.org/type/index.html is linked in two places in the spec. It's not really clear to me in what way they are "optional" or how they relate to the spec, but I'd accept your comment as an answer if you want – jberryman Aug 28 '19 at 01:18
3

It is difficult to compare YAML 1.2 and YAML 1.1 in this area. YAML 1.2 has schemas which are absent in YAML 1.1.

None of the schema's mentioned in the YAML 1.2 specification (base, json, core) mention Yes as a boolean type, and the examples don't use these anymore, where the YAML 1.1 still had these.

The YAML 1.2 specification however does mention possible schema's beyond the core schema, including parts of the language independent type repository Unfortunately there are areas in that repository, that contradict YAML 1.2 (such as how octals are represented). So a generic fully 1.1 compatible in YAML 1.2 is impossible.

Given the non-mention of Yes as a boolean, and the general confusion it caused (there are question about why Yes was quoted when dumped here on SO), I decided to drop support for that when I implemented YAML 1.2 support in ruamel.yaml. Other, less confusing (IMO) and useful things like the merge key (<<) are in ruamel.yaml (and so are less useful elements like the value key).

PyYAML however only supports the YAML 1.1 standard (that was replaced in 2009).

If your document isn't implicit, but has a header:

%YAML 1.1
---
x: yes

Then ruamel.yaml will load yes as boolean as well, as ruamel.yaml defaults to loading YAML 1.2 by default, whereas PyYAML still only (partly) supports loading of YAML 1.1

Anthon
  • 69,918
  • 32
  • 186
  • 246
-3

yaml 1.1 and 1.2

Both yaml 1.1 and 1.2 are being used nowadays, so it is possible to have parser for 1.2 and syntax highlight for 1.1 at the same time, which may be confusing.

For example, syntax/yaml.vim in neovim0.8.2 is for yaml 1.2, and it involves 3 schemes: json, core, pyyaml. (Note also failsave) It may answer this:

my editor is even highlighting "yes/no" as special

Note the line syn keyword yamlBool true True TRUE false False FALSE yes Yes YES no No NO on On ON off Off OFF contained containedin=@yamlScalarWithSpecials

enter image description here

pyyaml: based on yaml 1.1, and is working in progress to add support for the YAML 1.2 Core and JSON schemas

pyyaml is popular:

ruamel-yaml : a YAML 1.2 loader/dumper package for Python

it is a derivative of pyyaml 3.11 (as on 2023-01-29, the latest pyyaml is 6.0). It's on sourceForge instead of github.

Good Pen
  • 625
  • 6
  • 10
  • Please provide textual info as text, not as picture of text. Apart from that I do not see how "they are keywords" answers the question "Are they aliases?". With your infol, "Yes" and "True" are as much aliases as "False" and True". – Yunnosch Jan 29 '23 at 23:23
  • IMO, a picture is better than text here. – Good Pen Jan 30 '23 at 07:37
  • Please note the `my editor is even highlighting "yes/no" as special` in the OP. – Good Pen Jan 30 '23 at 07:45