File: duplicate-keys.md

package info (click to toggle)
python-strictyaml 1.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,708 kB
  • sloc: python: 12,836; sh: 48; makefile: 3
file content (85 lines) | stat: -rw-r--r-- 1,586 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
title: Duplicate keys
---


Duplicate keys are allowed in regular YAML - as parsed by pyyaml, ruamel.yaml and poyo:

Not only is it unclear whether x should be "cow" or "bull" (the parser will decide 'bull', but did you know that?),
if there are 200 lines between x: cow and x: bull, a user might very likely change the *first* x and erroneously believe
that the resulting value of x has been changed - when it has not.

In order to avoid all possible confusion, StrictYAML will simply refuse to parse this and will only accept associative
arrays where all of the keys are unique. It will throw a DuplicateKeysDisallowed exception.


Example yaml_snippet:

```yaml
a: cow
a: bull

```


```python
from strictyaml import load, DuplicateKeysDisallowed

```



Nameless exception:


```python
load(yaml_snippet)
```


```python
strictyaml.exceptions.DuplicateKeysDisallowed:
While parsing
  in "<unicode string>", line 2, column 1:
    a: bull
    ^ (line: 2)
Duplicate key 'a' found
  in "<unicode string>", line 2, column 2:
    a: bull
     ^ (line: 2)
```




Named exception:


```python
load(yaml_snippet, label="mylabel")
```


```python
strictyaml.exceptions.DuplicateKeysDisallowed:
While parsing
  in "mylabel", line 2, column 1:
    a: bull
    ^ (line: 2)
Duplicate key 'a' found
  in "mylabel", line 2, column 2:
    a: bull
     ^ (line: 2)
```







!!! note "Executable specification"

    Documentation automatically generated from 
    <a href="https://github.com/crdoconnor/strictyaml/blob/master/hitch/story/duplicatekeys.story">duplicatekeys.story
    storytests.