From 1c746be535f6b363bde3b85fe1cc9dfe7fbee370 Mon Sep 17 00:00:00 2001 From: Adam Olczak Date: Sun, 7 Sep 2025 16:42:04 +0200 Subject: [PATCH] Include None value from second object while merging dicts (#622, #664) --- pylsp/_utils.py | 2 ++ test/test_utils.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pylsp/_utils.py b/pylsp/_utils.py index dfe84b14..a5ca0dfc 100644 --- a/pylsp/_utils.py +++ b/pylsp/_utils.py @@ -167,6 +167,8 @@ def _merge_dicts_(a, b): yield (key, a[key]) elif key in a: yield (key, a[key]) + elif key in b and key not in a: + yield (key, b[key]) elif b[key] is not None: yield (key, b[key]) diff --git a/test/test_utils.py b/test/test_utils.py index 7ed6214f..26fdf16f 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -202,6 +202,22 @@ def test_merge_dicts() -> None: {"a": True, "b": {"x": 123, "y": {"hello": "world"}}}, {"a": False, "b": {"y": [], "z": 987}}, ) == {"a": False, "b": {"x": 123, "y": [], "z": 987}} + assert _utils.merge_dicts( + {"a": True, "b": "foo"}, + {"c": None} + ) == {"a": True, "b": "foo", "c": None}, "None value for key that is only in second object should be preserved" + assert _utils.merge_dicts( + {"a": None, "b": "foo"}, + {"b": "bar"} + ) == {"a": None, "b": "bar"}, "None value for key that is only in first object should be preserved" + assert _utils.merge_dicts( + {"a": True, "b": "foo"}, + {"b": None, } + ) == {"a": True, "b": "foo"}, "None value for key in second object should not override the value in first object" + assert _utils.merge_dicts( + {"a": True, "b": {"bar": "baz"}}, + {"b": {"bar": "baz", "foo": None}, } + ) == {"a": True, "b": {"bar": "baz", "foo": None}}, "Nested None value for key that is only in second object should be preserved" def test_clip_column() -> None: