根据 YAML 1.1 规范,'yes' 真的是 'true' 的别名吗? 1.2规格?

Is 'yes' really an alias for 'true' according to the YAML 1.1 spec? The 1.2 spec?

我正在尝试调试一个问题,归结为...

>>> 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

rumors on the internet关于"yes"和"no"是保留字,也分别是truefalse的同义词。

但是一共只有passing mention in the 1.1 spec but no elaboration, and the string "yes" doesn't appear in the 1.2 spec

其实翻看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,在改版后去掉了。

我想我在写它的过程中已经回答了我自己的问题,但是...这个关于 "yes/no" 的业务是否只是胡说八道而进入了实施(我的编辑甚至强调 "yes/no" 作为特殊),还是我误解或遗漏了规范的一部分?

在 yaml 规范 1.1 中将 yes/no 解释为 true/false 是有意设计的 documented。但是在 yaml 规范 2.2 中,将 yes/no 解释为 true/false 被删除了。

来自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

很难在这方面比较 YAML 1.2 和 YAML 1.1。 YAML 1.2 具有 YAML 1.1 中不存在的模式。

YAML 1.2 规范(基础,json,核心)中提到的模式的

None 提到 Yes 作为布尔类型,示例不再使用这些,YAML 1.1 仍然有这些。

然而,YAML 1.2 规范确实提到了核心模式之外的可能模式,包括 language independent type repository 的部分内容 不幸的是,该存储库中的某些区域与 YAML 1.2 相矛盾(例如八进制的表示方式)。所以在 YAML 1.2 中完全兼容 1.1 是不可能的。

考虑到 Yes 的 non-mention 作为布尔值,以及它引起的一般混乱(关于为什么 Yes 在转储到这里时被引用的问题),我决定当我在 ruamel.yaml 中实现 YAML 1.2 支持时放弃对它的支持。其他不那么令人困惑的 (IMO) 和有用的东西,如合并键 (<<) 在 ruamel.yaml 中(因此不太有用的元素,如值键)。

但 PyYAML 仅支持 YAML 1.1 标准(已于 2009 年取代)。

如果您的文档不是隐式的,但有 header:

%YAML 1.1
---
x: yes

然后 ruamel.yaml 也将加载 yes 作为布尔值,因为 ruamel.yaml 默认加载 YAML 1.2,而 PyYAML 仍然仅(部分)支持加载 YAML 1.1