Skip to content

Commit

Permalink
Fixed swap between dict items.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocaccamo committed Jul 15, 2022
1 parent 244f605 commit 37fed12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion benedict/core/swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
def swap(d, key1, key2):
if key1 == key2:
return
d[key1], d[key2] = d[key2], d[key1]
val1 = d[key1]
val1 = val1.copy() if isinstance(val1, dict) else val1
val2 = d[key2]
val2 = val2.copy() if isinstance(val2, dict) else val2
d[key1], d[key2] = val2, val1
16 changes: 16 additions & 0 deletions tests/dicts/test_benedict.py
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,22 @@ def test_swap_with_keypath(self):
b = benedict(d)
b.swap("a.y", "b.y")
b.swap("b.x", "c.x")
r = {
"a": {
"x": 1,
"y": 2,
},
"b": {
"x": 3,
"y": 1,
},
"c": {
"x": 2,
"y": 3,
},
}
self.assertEqual(b, r)

b.swap("a", "c")
r = {
"a": {
Expand Down

0 comments on commit 37fed12

Please sign in to comment.