Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dpath/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ def f(obj, pair, result):
(path, found) = pair

if keeper(path, found):
# A selected parent already includes its descendants by reference.
# Reassigning them is unnecessary and would try to mutate tuples.
if segments.has(result, path) and segments.get(result, path) is found:
return
segments.set(result, path, found, hints=segments.types(obj, path))

return segments.fold(obj, f, {})
Expand Down
11 changes: 11 additions & 0 deletions tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
from dpath import MergeType


def test_merge_preserves_tuples_in_source_lists():
dst = {"foo": []}
src = {"foo": [("bar", "baz")]}

dpath.merge(dst, src)

assert dst == src
assert dst["foo"][0] is src["foo"][0]
assert isinstance(src["foo"][0], tuple)


def test_merge_typesafe_and_separator():
src = {
"dict": {
Expand Down
10 changes: 10 additions & 0 deletions tests/test_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import dpath


def test_search_preserves_selected_tuple_container():
obj = {"foo": [("bar", {"baz": 1})]}

result = dpath.search(obj, "**")

assert result == obj
assert result["foo"] is obj["foo"]
assert isinstance(obj["foo"][0], tuple)


def test_search_paths_with_separator():
dict = {
"a": {
Expand Down