Skip to content

Commit

Permalink
fix(utils.dictionary): use deep copy instead of assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
loqusion committed May 7, 2024
1 parent fa60954 commit 3be55a1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/hyprshade/utils/dictionary.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from copy import deepcopy
from typing import Literal, TypeAlias

DeepMergeStrategy: TypeAlias = Literal["force", "keep"]
Expand All @@ -13,9 +14,9 @@ def __deep_merge_impl(
if isinstance(dest[key], dict) and isinstance(source[key], dict):
__deep_merge_impl(dest[key], source[key], strategy=strategy)
elif strategy == "force":
dest[key] = source[key]
dest[key] = deepcopy(source[key])
else:
dest[key] = source[key]
dest[key] = deepcopy(source[key])

return dest

Expand Down
6 changes: 6 additions & 0 deletions tests/utils/test_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ def test_referential_equality(self):
d: dict = {}
assert deep_merge(d, {"foo": "bar"}, strategy="force") is d
assert deep_merge(d, {"baz": "qux"}, strategy="keep") is d

def test_referential_inequality(self):
d: dict = {}
a = [1, 2, 3]
deep_merge(d, {"foo": a})
assert d["foo"] is not a

0 comments on commit 3be55a1

Please sign in to comment.