Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(option list): fix crash on empty option list with auto width #5492

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/textual/widgets/_option_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/option_list/test_option_list_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading