`Matcher("")` raises `ValueError` instead of matching nothing
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
Research direction
The crash comes from score() in textual/fuzzy.py, which unpacks last_offset, *offsets = positions when the list is empty; the empty-string query reaches it through the substring fast path in _match. Start by reading Matcher.match and FuzzySearch.match to see how the empty query flows through, then decide where an empty query should return a zero score. Done when Matcher("").match("Toggle dark mode") returns 0 without raising, and a test in the fuzzy test module covers the empty query.
Written by the indexing model from the issue text.
Description
Hi! I hit this while writing generated tests against Textual's widgets.
textual.fuzzy.Matcher (and FuzzySearch) raise ValueError when the query is empty:
from textual.fuzzy import Matcher
print(Matcher("").match("Toggle dark mode"))
Output (the same on 8.2.7 and on main at 5e5b7ef):
File ".../textual/fuzzy.py", line 201, in match
return self.fuzzy_search.match(self.query, candidate)[0]
File ".../textual/fuzzy.py", line 58, in match
result = max(self._match(query, candidate), key=itemgetter(0), default=default)
File ".../textual/fuzzy.py", line 109, in _match
score(candidate, offsets) * (2.0 if candidate == query else 1.5),
File ".../textual/fuzzy.py", line 83, in score
last_offset, *offsets = positions
ValueError: not enough values to unpack (expected at least 1, got 0)
"" in candidate is always true, so _match takes the substring fast path with an empty list of offsets, and score() unpacks the first of them.
The command palette never searches for an empty string (it calls discover() instead), so this only bites code that uses Matcher or Provider.matcher() directly, for example a widget that filters a list as the user types and calls the matcher before anything is typed. I'd expect an empty query to give a score of 0, as any other non-matching query does.
Textual 8.2.7 and main (5e5b7ef), Python 3.13, macOS.
- Dominant language
- Python
- Stars
- 37.7k
- Forks
- 1.5k
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Textualize/textual
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6790 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
Textualize/textual#6789 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6787 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6782 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Textualize/textual#6778 · 1 comment ·
All issues in Textualize/textual
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mohanraj00/entelos#200 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
sweep
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
bketelsen/truenas-mcp#77 ·
Maintainers usually reply within 1 day
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskOpen
Difficulty 1/5 Under an hour Newbie friendliness 86/100
sandbox-quantum/switch#2163 ·
Maintainers usually reply within 1 day