`Matcher("")` raises `ValueError` instead of matching nothing

Open Beginner friendly
#6,776 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
cli

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/textual

All issues in Textualize/textual

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.