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

Aperta Adatta ai principianti
#6,776 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
cli

Direzione di ricerca

L'arresto anomalo ha origine in score() in textual/fuzzy.py, che scompatta last_offset, *offsets = positions quando l'elenco è vuoto; la query con stringa vuota arriva fin lì attraverso il percorso rapido per le sottostringhe in _match. Inizia leggendo Matcher.match e FuzzySearch.match per vedere come viene propagata la query vuota, poi decidi dove una query vuota dovrebbe restituire un punteggio pari a zero. Il lavoro è completo quando Matcher("").match("Toggle dark mode") restituisce 0 senza generare un'eccezione e un test nel modulo di test fuzzy copre la query vuota.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
37.7k
Fork
1.5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Textualize/textual

Tutte le issue di Textualize/textual

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.