Allow zero items to be passed to add_options and do zero work

This commit is contained in:
Dave Pearson
2023-05-07 18:01:56 +01:00
parent b44983e8d4
commit 010356a23e
2 changed files with 17 additions and 10 deletions

View File

@@ -520,6 +520,9 @@ class OptionList(ScrollView, can_focus=True):
Raises: Raises:
DuplicateID: If there is an attempt to use a duplicate ID. DuplicateID: If there is an attempt to use a duplicate ID.
""" """
# Only work if we have items to add; but don't make a fuss out of
# zero items to add, just carry on like nothing happened.
if items:
# Turn any incoming values into valid content for the list. # Turn any incoming values into valid content for the list.
content = [self._make_content(item) for item in items] content = [self._make_content(item) for item in items]
self._contents.extend(content) self._contents.extend(content)

View File

@@ -110,6 +110,10 @@ async def test_add_later() -> None:
[Option("more still"), "Yet more options", "so many options!"] [Option("more still"), "Yet more options", "so many options!"]
) )
assert option_list.option_count == 10 assert option_list.option_count == 10
option_list.add_option(None)
assert option_list.option_count == 10
option_list.add_options([])
assert option_list.option_count == 10
async def test_create_with_duplicate_id() -> None: async def test_create_with_duplicate_id() -> None: