Debian Codegraph @

hitch/story/duplicatekeys.story

raw
1 Duplicate keys:
2 docs: restrictions/duplicate-keys
3 based on: strictyaml
4 description: |
5 Duplicate keys are allowed in regular YAML - as parsed by pyyaml, ruamel.yaml and poyo:
6
7 Not only is it unclear whether x should be "cow" or "bull" (the parser will decide 'bull', but did you know that?),
8 if there are 200 lines between x: cow and x: bull, a user might very likely change the *first* x and erroneously believe
9 that the resulting value of x has been changed - when it has not.
10
11 In order to avoid all possible confusion, StrictYAML will simply refuse to parse this and will only accept associative
12 arrays where all of the keys are unique. It will throw a DuplicateKeysDisallowed exception.
13 given:
14 setup: |
15 from strictyaml import load, DuplicateKeysDisallowed
16 yaml_snippet: |
17 a: cow
18 a: bull
19 variations:
20 Nameless exception:
21 steps:
22 - Run:
23 code: load(yaml_snippet)
24 raises:
25 type: strictyaml.exceptions.DuplicateKeysDisallowed
26 message: |-
27 While parsing
28 in "<unicode string>", line 2, column 1:
29 a: bull
30 ^ (line: 2)
31 Duplicate key 'a' found
32 in "<unicode string>", line 2, column 2:
33 a: bull
34 ^ (line: 2)
35
36
37 Named exception:
38 steps:
39 - Run:
40 code: load(yaml_snippet, label="mylabel")
41 raises:
42 type: strictyaml.exceptions.DuplicateKeysDisallowed
43 message: |-
44 While parsing
45 in "mylabel", line 2, column 1:
46 a: bull
47 ^ (line: 2)
48 Duplicate key 'a' found
49 in "mylabel", line 2, column 2:
50 a: bull
51 ^ (line: 2)