Repository navigation
Commit ac3b04e
Keep unwrap() key order after a value is replaced (#591)
* Keep unwrap() key order after a value is replaced
_replace_at() removes the key from _map and re-inserts it, which moves it
to the end of the dict's insertion order while _body keeps the original
slot. dumps() and keys() walk _body and stay correct; unwrap() walks _map
and does not:
doc = parse("a = 1\nb = 2\nc = 3\n")
doc["b"] = 9
dumps(doc) # 'a = 1\nb = 9\nc = 3\n'
list(doc.keys()) # ['a', 'b', 'c']
list(doc.unwrap()) # ['a', 'c', 'b']
unwrap() moved to _map in #521 on the stated assumption that it "iterates
in the same insertion order as the old self.items()", which a replacement
breaks. Order comes from the body index instead, so the _map fast path is
kept and dumps() stays the reference.
Replacements that genuinely move an element still move: a bare key
promoted to a table has to be emitted after the inline entries, and
unwrap() follows the body there too. Both cases are covered.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>1 parent 49d6efa commit ac3b04e
2 files changed
Lines changed: 28 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
163 | 184 | | |
164 | 185 | | |
165 | 186 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
71 | | - | |
72 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
73 | 77 | | |
74 | 78 | | |
75 | 79 | | |
| |||
0 commit comments