diff --git a/deepmerge/merger.py b/deepmerge/merger.py index c1f747e..5ccd568 100644 --- a/deepmerge/merger.py +++ b/deepmerge/merger.py @@ -36,9 +36,9 @@ def type_conflict_strategy(self, *args): return self._type_conflict_strategy(self, *args) def value_strategy(self, path, base, nxt): - if not (isinstance(base, type(nxt)) or isinstance(nxt, type(base))): - return self.type_conflict_strategy(path, base, nxt) for typ, strategy in self._type_strategies: - if isinstance(nxt, typ): + if isinstance(base, typ) and isinstance(nxt, typ): return strategy(self, path, base, nxt) + if not (isinstance(base, type(nxt)) or isinstance(nxt, type(base))): + return self.type_conflict_strategy(path, base, nxt) return self._fallback_strategy(self, path, base, nxt) diff --git a/deepmerge/tests/test_full.py b/deepmerge/tests/test_full.py index c7710cd..799db19 100644 --- a/deepmerge/tests/test_full.py +++ b/deepmerge/tests/test_full.py @@ -1,4 +1,5 @@ from deepmerge.exception import * +from collections import OrderedDict, defaultdict import pytest @@ -48,3 +49,12 @@ def test_example(): always_merger.merge(base, next) assert base == {"foo": "value", "bar": "value2", "baz": ["a", "b"]} + + +def test_subtypes(): + base = OrderedDict({"foo": "value", "baz": ["a"]}) + next = defaultdict(str, {"bar": "value2", "baz": ["b"]}) + + result = always_merger.merge(base, next) + + assert dict(result) == {"foo": "value", "bar": "value2", "baz": ["a", "b"]}