Skip to content

Commit d206ac6

Browse files
authored
Merge branch 'main' into fix-4104
2 parents 3697157 + e686b73 commit d206ac6

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Fixed
1111

1212
- Fixed a hang in the Linux driver when connected to a pipe https://github.com/Textualize/textual/issues/4104
13+
- Fixed broken `OptionList` `Option.id` mappings https://github.com/Textualize/textual/issues/4101
1314

1415
### Changed
1516

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)