diff --git a/src/textual/widgets/_option_list.py b/src/textual/widgets/_option_list.py index d6e6af537..f72c8b7d8 100644 --- a/src/textual/widgets/_option_list.py +++ b/src/textual/widgets/_option_list.py @@ -520,16 +520,19 @@ class OptionList(ScrollView, can_focus=True): Raises: DuplicateID: If there is an attempt to use a duplicate ID. """ - # Turn any incoming values into valid content for the list. - content = [self._make_content(item) for item in items] - self._contents.extend(content) - # Pull out the content that is genuine options and add them to the - # list of options. - options = [item for item in content if isinstance(item, Option)] - if options: - self._options.extend(options) - self._refresh_content_tracking(force=True) - self.refresh() + # 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. + content = [self._make_content(item) for item in items] + self._contents.extend(content) + # Pull out the content that is genuine options and add them to the + # list of options. + options = [item for item in content if isinstance(item, Option)] + if options: + self._options.extend(options) + self._refresh_content_tracking(force=True) + self.refresh() return self def add_option(self, item: NewOptionListContent = None) -> Self: diff --git a/tests/option_list/test_option_list_create.py b/tests/option_list/test_option_list_create.py index 043e3e584..7e0bbc49c 100644 --- a/tests/option_list/test_option_list_create.py +++ b/tests/option_list/test_option_list_create.py @@ -110,6 +110,10 @@ async def test_add_later() -> None: [Option("more still"), "Yet more options", "so many options!"] ) 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: