Skip to content

Commit e686b73

Browse files
authored
Merge pull request #4103 from davep/broken-optionlist-ids
Fix broken OptionList Option id mapping
2 parents 8ae0b27 + bf87df3 commit e686b73

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## Unreleased
99

10+
### Fixed
11+
12+
- Fixed broken `OptionList` `Option.id` mappings https://github.com/Textualize/textual/issues/4101
13+
1014
### Changed
1115

1216
- Breaking change: keyboard navigation in `RadioSet`, `ListView`, `OptionList`, and `SelectionList`, no longer allows highlighting disabled items https://github.com/Textualize/textual/issues/3881

src/textual/widgets/_option_list.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,15 +573,16 @@ def add_options(self, items: Iterable[NewOptionListContent]) -> Self:
573573
content = [self._make_content(item) for item in items]
574574
self._duplicate_id_check(content)
575575
self._contents.extend(content)
576-
# Pull out the content that is genuine options. Add them to the
577-
# list of options and map option IDs to their new indices.
576+
# Pull out the content that is genuine options, create any new
577+
# ID mappings required, then add the new options to the option
578+
# list.
578579
new_options = [item for item in content if isinstance(item, Option)]
579-
self._options.extend(new_options)
580580
for new_option_index, new_option in enumerate(
581581
new_options, start=len(self._options)
582582
):
583583
if new_option.id:
584584
self._option_ids[new_option.id] = new_option_index
585+
self._options.extend(new_options)
585586

586587
self._refresh_content_tracking(force=True)
587588
self.refresh()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Tests inspired by https://github.com/Textualize/textual/issues/4101"""
2+
3+
from __future__ import annotations
4+
5+
from textual.app import App, ComposeResult
6+
from textual.widgets import OptionList
7+
from textual.widgets.option_list import Option
8+
9+
10+
class OptionListApp(App[None]):
11+
"""Test option list application."""
12+
13+
def compose(self) -> ComposeResult:
14+
yield OptionList()
15+
16+
17+
async def test_get_after_add() -> None:
18+
"""It should be possible to get an option by ID after adding."""
19+
async with OptionListApp().run_test() as pilot:
20+
option_list = pilot.app.query_one(OptionList)
21+
option_list.add_option(Option("0", id="0"))
22+
assert option_list.get_option("0").id == "0"

0 commit comments

Comments
 (0)