diff --git a/CHANGELOG.md b/CHANGELOG.md index ab70eb5340..68a9171e28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed special case with calculating the height of a container where all children have dynamic heights https://github.com/Textualize/textual/pull/5463 - Fixed scrollbars ignoring background opacity https://github.com/Textualize/textual/issues/5458 - Fixed `Header` icon showing command palette tooltip when disabled https://github.com/Textualize/textual/pull/5427 +- Fixed crash on empty `OptionList` with auto width https://github.com/Textualize/textual/issues/5489 ## [1.0.0] - 2024-12-12 diff --git a/src/textual/widgets/_option_list.py b/src/textual/widgets/_option_list.py index 2b895797c8..092f28de4b 100644 --- a/src/textual/widgets/_option_list.py +++ b/src/textual/widgets/_option_list.py @@ -391,6 +391,8 @@ def _populate(self) -> None: def get_content_width(self, container: Size, viewport: Size) -> int: """Get maximum width of options.""" + if not self._options: + return 0 console = self.app.console options = console.options padding = self.get_component_styles("option-list--option").padding diff --git a/tests/option_list/test_option_list_create.py b/tests/option_list/test_option_list_create.py index 69ccce06dc..daf98b53e6 100644 --- a/tests/option_list/test_option_list_create.py +++ b/tests/option_list/test_option_list_create.py @@ -161,3 +161,21 @@ async def test_options_are_available_soon() -> None: option = Option("", id="some_id") option_list = OptionList(option) assert option_list.get_option("some_id") is option + + +async def test_empty_option_list_with_auto_width_doesnt_crash() -> None: + """Regression test for https://github.com/Textualize/textual/issues/5489""" + + class EmptyOptionListApp(App): + CSS = """ + OptionList { + width: auto; + } + """ + + def compose(self) -> ComposeResult: + yield OptionList() + + async with EmptyOptionListApp().run_test() as pilot: + # If no exception is raised, this test will pass. + option_list = pilot.app.query_one(OptionList)