From ec3ade97d82b6b28ac5d738cd4c441574e8fd8ab Mon Sep 17 00:00:00 2001 From: fhgffy <102001626+fhgffy@users.noreply.github.com> Date: Sat, 10 Oct 2026 00:33:14 +0800 Subject: [PATCH] Construct BoxList without testing the input truth value --- AUTHORS.rst | 2 ++ CHANGES.rst | 5 +++++ box/box_list.py | 3 ++- test/test_box_list.py | 16 ++++++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 6b44851..3c6321f 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -5,6 +5,8 @@ reporting, all input is greatly appreciated! Code contributions: +- fhgffy + - Alexandre Decan (AlexandreDecan) - dhilipsiva (dhilipsiva) - MAA (FooBarQuaxx) diff --git a/CHANGES.rst b/CHANGES.rst index c4a347d..4de668e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,11 @@ Changelog ========= +Unreleased +---------- + +* Fix BoxList construction from iterable inputs with false or ambiguous truth values. + Version 7.4.1 ------------- diff --git a/box/box_list.py b/box/box_list.py index 72609ff..91b42ae 100644 --- a/box/box_list.py +++ b/box/box_list.py @@ -53,7 +53,8 @@ def __init__(self, iterable: Iterable | None = None, box_class: type[box.Box] = self.box_options = box_options self.box_options["box_class"] = box_class self.box_org_ref = iterable - if iterable: + # 2026-10-10: Iteration does not require a truth value. + if iterable is not None: for x in iterable: self.append(x) self.box_org_ref = None diff --git a/test/test_box_list.py b/test/test_box_list.py index 9cd90e0..afb7fc8 100644 --- a/test/test_box_list.py +++ b/test/test_box_list.py @@ -50,6 +50,22 @@ def test_box_list(self): assert isinstance(new_list[1], BoxList) assert not isinstance(new_list.to_list(), BoxList) + @pytest.mark.parametrize("falsey", [False, True]) + def test_box_list_does_not_require_iterable_truth_value(self, falsey): + # 2026-10-10: Iterable inputs can be falsey or have no boolean value. + class Rows: + def __iter__(self): + return iter([{"value": 1}]) + + def __bool__(self): + if falsey: + return False + raise ValueError("ambiguous truth value") + + result = BoxList(Rows()) + assert len(result) == 1 + assert result[0].value == 1 + def test_frozen_list(self): bl = BoxList([5, 4, 3], frozen_box=True) with pytest.raises(BoxError):