| 1 |
--- |
| 2 |
title: The Norway Problem - why StrictYAML refuses to do implicit typing and so should you |
| 3 |
--- |
| 4 |
|
| 5 |
A while back I met an old coworker and he started telling me about this |
| 6 |
interesting bug he faced: |
| 7 |
|
| 8 |
"So, we started internationalizing the website by creating a config |
| 9 |
file. We added the UK, Ireland, France and Germany at first." |
| 10 |
|
| 11 |
```yaml |
| 12 |
countries: |
| 13 |
- GB |
| 14 |
- IE |
| 15 |
- FR |
| 16 |
- DE |
| 17 |
``` |
| 18 |
|
| 19 |
"This was all fine. However, one day after a quick configuration change |
| 20 |
all hell broke loose. It turned out that while the UK, France and |
| 21 |
Germany were all fine, *Norway* was *not*..." |
| 22 |
|
| 23 |
"While the website went down and we were losing money we chased down |
| 24 |
a number of loose ends until finally finding the root cause." |
| 25 |
|
| 26 |
"If turned out that if feed this configuration file into |
| 27 |
[pyyaml](http://pyyaml.org):" |
| 28 |
|
| 29 |
```yaml |
| 30 |
countries: |
| 31 |
- GB |
| 32 |
- IE |
| 33 |
- FR |
| 34 |
- DE |
| 35 |
- NO |
| 36 |
``` |
| 37 |
|
| 38 |
"This is what you got in return:" |
| 39 |
|
| 40 |
```python |
| 41 |
>>> from pyyaml import load |
| 42 |
>>> load(the_configuration) |
| 43 |
{'countries': ['GB', 'IE', 'FR', 'DE', False]} |
| 44 |
``` |
| 45 |
|
| 46 |
It snows a *lot* in False. |
| 47 |
|
| 48 |
When this is fed to code that expects a string of the form 'NO', |
| 49 |
then the code will usually break, often with a cryptic error, |
| 50 |
Typically it would be a KeyError when trying to use 'False' |
| 51 |
as a key in a dict when no such key exists. |
| 52 |
|
| 53 |
It can be "quick fixed" by using quotes - a fix for sure, but |
| 54 |
kind of a hack - and by that time the damage is done: |
| 55 |
|
| 56 |
```yaml |
| 57 |
countries: |
| 58 |
- GB |
| 59 |
- IE |
| 60 |
- FR |
| 61 |
- DE |
| 62 |
- 'NO' |
| 63 |
``` |
| 64 |
|
| 65 |
The most tragic aspect of this bug, however, is that it is |
| 66 |
*intended* behavior according to the [YAML 1.2 specification](https://yaml.org/spec/1.2.2/). |
| 67 |
The real fix requires explicitly disregarding the spec - which |
| 68 |
is why most YAML parsers have it. |
| 69 |
|
| 70 |
StrictYAML sidesteps this problem by ignoring key parts of the |
| 71 |
spec, in an attempt to create a "zero surprises" parser. |
| 72 |
|
| 73 |
*Everything* is a string by default: |
| 74 |
|
| 75 |
```python |
| 76 |
>>> from strictyaml import load |
| 77 |
>>> load(the_configuration).data |
| 78 |
{'countries': ['GB', 'IE', 'FR', 'DE', 'NO']} |
| 79 |
``` |
| 80 |
|
| 81 |
|
| 82 |
## String or float? |
| 83 |
|
| 84 |
Norway is just the tip of the iceberg. The first time this problem hit me |
| 85 |
I was maintaining a configuration file of application versions. I had |
| 86 |
a file like this initially - which caused no issues: |
| 87 |
|
| 88 |
```yaml |
| 89 |
python: 3.5.3 |
| 90 |
postgres: 9.3.0 |
| 91 |
``` |
| 92 |
|
| 93 |
However, if I changed it *very* slightly: |
| 94 |
|
| 95 |
```yaml |
| 96 |
python: 3.5.3 |
| 97 |
postgres: 9.3 |
| 98 |
``` |
| 99 |
|
| 100 |
I started getting type errors because it was parsed like this: |
| 101 |
|
| 102 |
```python |
| 103 |
>>> from ruamel.yaml import load |
| 104 |
>>> load(versions) == [{"python": "3.5.3", "postgres": 9.3}] # oops those *both* should have been strings |
| 105 |
``` |
| 106 |
|
| 107 |
Again, this led to type errors in my code. Again, I 'quick fixed' it with quotes. |
| 108 |
However, the solution I really wanted was: |
| 109 |
|
| 110 |
```python |
| 111 |
>>> from strictyaml import load |
| 112 |
>>> load(versions) == [{"python": "3.5.3", "postgres": "9.3"}] # that's better |
| 113 |
``` |
| 114 |
|
| 115 |
|
| 116 |
## The world's most buggy name |
| 117 |
|
| 118 |
[Christopher Null](http://www.wired.com/2015/11/null) has a name that is |
| 119 |
notorious for breaking software code - airlines, banks, every bug caused |
| 120 |
by a programmer who didn't know a type from their elbow has hit him. |
| 121 |
|
| 122 |
YAML, sadly, is no exception: |
| 123 |
|
| 124 |
```yaml |
| 125 |
first name: Christopher |
| 126 |
surname: Null |
| 127 |
``` |
| 128 |
|
| 129 |
```python |
| 130 |
# Is it okay if we just call you Christopher None instead? |
| 131 |
>>> load(name) == {"first name": "Christopher", "surname": None} |
| 132 |
``` |
| 133 |
|
| 134 |
With StrictYAML: |
| 135 |
|
| 136 |
```python |
| 137 |
>>> from strictyaml import load |
| 138 |
>>> load(name) == {"first name": "Christopher", "surname": "Null"} |
| 139 |
``` |
| 140 |
|
| 141 |
|
| 142 |
## Type theoretical concerns |
| 143 |
|
| 144 |
Type theory is a popular topic with regards to programming languages, |
| 145 |
where a well designed type system is regarded (rightly) as a yoke that |
| 146 |
can catch bugs at an early stage of development while *poorly* |
| 147 |
designed type systems provide fertile breeding ground for edge case |
| 148 |
bugs. |
| 149 |
|
| 150 |
(it's equally true that extremely strict type systems require a lot |
| 151 |
more upfront and the law of diminishing returns applies to type |
| 152 |
strictness - a cogent answer to the question "why is so little |
| 153 |
software written in haskell?"). |
| 154 |
|
| 155 |
A less popular, although equally true idea is the notion that markup |
| 156 |
languages like YAML have the same issues with types - as demonstrated |
| 157 |
above. |
| 158 |
|
| 159 |
|
| 160 |
## User Experience |
| 161 |
|
| 162 |
In a way, type systems can be considered both a mathematical concern |
| 163 |
and a UX device. |
| 164 |
|
| 165 |
In the above, and in most cases, implicit typing represents a major violation |
| 166 |
of the UX [principle of least astonishment](https://en.wikipedia.org/wiki/Principle_of_least_astonishment). |