Skip to content

Commit bf4e0b9

Browse files
committed
Bugfix Select.selection with no value selected
Based on the docs Select.selection should return None if no value is selected, i.e. the value is Select.Blank. It just raised an assertion error though. Fixed the issue and added a test
1 parent d9f7ffd commit bf4e0b9

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212
- Static and Label now accept Content objects, satisfying type checkers https://github.com/Textualize/textual/pull/5618
1313
- Fixed click selection not being disabled when allow_select was set to false https://github.com/Textualize/textual/issues/5627
1414
- Fixed crash on clicking line API border https://github.com/Textualize/textual/pull/5641
15+
- Fixed Select.selection now correctly returns None if Select.BLANK is selected instead of an AssertionError
1516

1617
### Added
1718

src/textual/widgets/_select.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,8 @@ def selection(self) -> SelectType | None:
477477
478478
"""
479479
value = self.value
480-
assert not isinstance(value, NoSelection)
480+
if isinstance(value, NoSelection):
481+
return None
481482
return value
482483

483484
def _setup_variables_for_options(

tests/select/test_blank_and_clear.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,13 @@ def compose(self):
6363
assert not select.is_blank()
6464
with pytest.raises(InvalidSelectValueError):
6565
select.clear()
66+
67+
async def test_selection_is_none_with_blank():
68+
class SelectApp(App[None]):
69+
def compose(self):
70+
yield Select(SELECT_OPTIONS)
71+
72+
app = SelectApp()
73+
async with app.run_test():
74+
select = app.query_one(Select)
75+
assert select.selection is None

0 commit comments

Comments
 (0)