Skip to content

Commit 1c746be

Browse files
committed
Include None value from second object while merging dicts (#622, #664)
1 parent 89265cd commit 1c746be

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

pylsp/_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ def _merge_dicts_(a, b):
167167
yield (key, a[key])
168168
elif key in a:
169169
yield (key, a[key])
170+
elif key in b and key not in a:
171+
yield (key, b[key])
170172
elif b[key] is not None:
171173
yield (key, b[key])
172174

test/test_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ def test_merge_dicts() -> None:
202202
{"a": True, "b": {"x": 123, "y": {"hello": "world"}}},
203203
{"a": False, "b": {"y": [], "z": 987}},
204204
) == {"a": False, "b": {"x": 123, "y": [], "z": 987}}
205+
assert _utils.merge_dicts(
206+
{"a": True, "b": "foo"},
207+
{"c": None}
208+
) == {"a": True, "b": "foo", "c": None}, "None value for key that is only in second object should be preserved"
209+
assert _utils.merge_dicts(
210+
{"a": None, "b": "foo"},
211+
{"b": "bar"}
212+
) == {"a": None, "b": "bar"}, "None value for key that is only in first object should be preserved"
213+
assert _utils.merge_dicts(
214+
{"a": True, "b": "foo"},
215+
{"b": None, }
216+
) == {"a": True, "b": "foo"}, "None value for key in second object should not override the value in first object"
217+
assert _utils.merge_dicts(
218+
{"a": True, "b": {"bar": "baz"}},
219+
{"b": {"bar": "baz", "foo": None}, }
220+
) == {"a": True, "b": {"bar": "baz", "foo": None}}, "Nested None value for key that is only in second object should be preserved"
205221

206222

207223
def test_clip_column() -> None:

0 commit comments

Comments
 (0)