From 8183d430951da61d1382a96307438530b0e5ffed Mon Sep 17 00:00:00 2001 From: Alexandre <46300952+alexbertho@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:58:48 +0100 Subject: [PATCH] Preserve one-shot pairs in ConfigBox.merge_update --- AUTHORS.rst | 1 + CHANGES.rst | 1 + box/config_box.py | 3 ++- test/test_config_box_case.py | 9 +++++++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 9f167759..bb07826b 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -5,6 +5,7 @@ reporting, all input is greatly appreciated! Code contributions: +- Alexandre Bertho (alexbertho) - Alexandre Decan (AlexandreDecan) - dhilipsiva (dhilipsiva) - MAA (FooBarQuaxx) diff --git a/CHANGES.rst b/CHANGES.rst index 237e0d98..cf117388 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,7 @@ Changelog Version 8.0.1 ------------- +* Fix ConfigBox.merge_update consuming one-shot key/value pairs before merging them * Fixing #349 BoxList item comments moving to the wrong items after a box_dots deletion, e.g. del bl["[0]"] (thanks to HocVoNgThai) * Fixing #349 BoxList.sort() calling the key function twice when the list has comments (thanks to HocVoNgThai) diff --git a/box/config_box.py b/box/config_box.py index 5d5ed876..9f7b37e8 100644 --- a/box/config_box.py +++ b/box/config_box.py @@ -203,7 +203,8 @@ def merge_update(self, *args, **kwargs): if hasattr(args[0], "keys"): incoming = list(args[0].keys()) else: - args = (list(args[0]),) # may be a one-shot iterator, it is read twice + # Both the outer iterable and its pairs may be one-shot iterators. + args = ([tuple(pair) for pair in args[0]],) incoming = [k for k, _ in args[0]] else: incoming = [] diff --git a/test/test_config_box_case.py b/test/test_config_box_case.py index 95f5f9c5..63530cc1 100644 --- a/test/test_config_box_case.py +++ b/test/test_config_box_case.py @@ -12,6 +12,15 @@ class TestConfigBoxCaseInsensitive: + @pytest.mark.parametrize("outer", [list, iter]) + def test_merge_update_one_shot_pairs(self, outer): + cb = ConfigBox({"Host": {"port": 80}}) + pairs = outer([iter(("host", {"name": "localhost"})), iter(("timeout", 30))]) + cb.merge_update(pairs) + assert cb.to_dict() == {"host": {"port": 80, "name": "localhost"}, "timeout": 30} + assert list(cb) == ["host", "timeout"] + assert cb.HOST.port == 80 + def test_lookup_any_case(self): cb = ConfigBox({"HOST": "localhost", "ServerPort": 8080}) assert cb["host"] == cb["Host"] == cb.host == cb.HOST == "localhost"