
YAML: The Norway Problem (2022) by carlos-menezes
Earlier this week, Haroen Viaene posted this tweet about YAML:
worst part of yaml: https://yaml.org/type/bool.html
— Haroen Viaene (@haroenv) January 10, 2022
The linked-to page contains the documentation on what defines a boolean in YAML, and details that it can be parsed using this regex:
y|Y|yes|Yes|YES|n|N|no|No|NO
|true|True|TRUE|false|False|FALSE
|on|On|ON|off|Off|OFF
~
The reason to why this is problematic in some cases, is “The Norway Problem” YAML has: when you abbreviate Norway to its ISO 3166-1 ALPHA-2 form NO
, YAML will return false
when parsing it:
countries:
- GB
- IE
- FR
- DE
- NO
>>> from pyyaml import
15 Comments
firesteelrain
This problem occurs because pyyaml load() uses the full YAML 1.1 schema. There is another function BaseLoader that will interpret everything as a string which is the workaround that the article suggests. Just another way to achieve it.
It’s a bit of a sore spot in the YAML community as to why PyYAML can’t / won’t support YAML 1.2. It was in maintenance mode for a while. YAML 1.2 also introduced breaking changes.
From a SO comment: “ As long as you're okay with the YAML 1.1 standard, PyYAML is still perfectly fine, secure, etc. If you want to support the YAML 1.2 spec (released in 2009), you can use ruamel.yaml, which started out as a fork of PyYAML. –
CrazyChucky
Commented Mar 26, 2023 at 20:51”
– https://stackoverflow.com/q/75850232
gnabgib
Related
The YAML document from hell (566 points, 2023, 353 comments) https://news.ycombinator.com/item?id=34351503
That's a Lot of YAML (429 points, 2023, 478 comments) https://news.ycombinator.com/item?id=37687060
No YAML (Same as above) (152 points, 2021, 149 comments) https://news.ycombinator.com/item?id=29019361
codr7
[flagged]
dissent
I reckon if this is really a big concern for anybody, then they are probably writing way too much YAML to begin with. If you're being caught out by things like this and need to debug it, then it maps very cleanly to types in most high level languages and you can generate your YAML from that instead.
ashishb
How often do people even encounter this issue?
I have been using YAML for 5+ years and have never had it before.
Further, I use `yamllint` which points this out as a lint issue "truthy value should be one of [false, true]".
thyrsus
I do a lot of ansible which needs to run on multiple versions, and their yaml typing are not consistent – whenever I have a variable in a logic statement, I nearly always need to apply the "| bool" filter.
quechimba
We had this issue many years ago when people from Norway couldn't sign up. Took us a while to figure out
singpolyma3
Quote your strings
umanwizard
“Be liberal in what you accept” rears its ugly head once more.
kazinator
In Lisp, if you want to read text into symbols (e.g. file of words), you just switch to a dedicated package in which those symbols are interned. Then if NIL happens to come up, it will be a symbol named "NIL" in that package, unrelated to the special object.
TZubiri
That edge case sounds like a reasonable tradeoff you would make for such a simple and readable generic data format.
Escaped json probably hits that sweetspot by being a bit uglier than yaml, but 100 times simpler than xml, though.
thund
I like using tags and avoid any doubt
!!boolean
https://dev.to/kalkwst/a-gentle-introduction-to-the-yaml-for…
nnurmanov
Another solution is to change the country name:)
riffraff
Usual reminder that this is not a problem in YAML 1.2 released 15 years ago.
Sadly many libraries still don't support it.
normie3000
Google App Engine used to do this to environment variables defined in YAML. IIRC it would convert the string "true" to "Yes", which was a fun surprise when deploying Java And NodeJS apps.