`MaskedInput` crashes with `AssertionError` when you type or delete with the cursor on a separator

Aperta Adatta ai principianti
#6,761 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
77/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
cli, frontend

Direzione di ricerca

Leggi MaskedInput e i suoi metodi _Template.insert_text_at_cursor e _Template.delete_at_position, insieme ai test esistenti dei widget. Riproduci il crash usando run_test() e pilot.press() con la maschera e il valore forniti. Completato quando digitare e cancellare nelle posizioni dei separatori non causa più un'asserzione e il comportamento atteso descritto è coperto dai test.

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

Descrizione

Hi! I hit this while writing generated tests against Textual's widgets.

If the cursor of a MaskedInput rests on a separator, typing a character or pressing delete crashes the app with an AssertionError. The easiest way to get there is a value that stops just before a separator, such as value="1234" for 9999-9999: the cursor goes to the end of the value, which is the -.

from textual.app import App, ComposeResult
from textual.widgets import MaskedInput

class MaskedApp(App):
    def compose(self) -> ComposeResult:
        yield MaskedInput("9999-9999", value="1234")

if __name__ == "__main__":
    MaskedApp().run()

Press end (or pass select_on_focus=False), then type 5.

Expected: the value becomes 1234-5, as it does when you type 12345 from empty.

Actual:

│ ❱ 247 │   │   │   assert _CharFlags.SEPARATOR not in char_definition.flags   │
AssertionError

I tried a few ways in, headless with run_test() and pilot.press(). Each line is the keys pressed and what happened:

9999-9999, value 1234, end, 5: AssertionError:
9999-9999, value 1234, select_on_focus=False, 5: AssertionError:
99/99, value 12, end, 3: AssertionError:
9999-9999, value 1234, end, delete: AssertionError:
(999) 999-9999, value (12, select_on_focus=False, home, 5: AssertionError:
9999-9999, value 1234-, end, 5 (control): value='1234-5' cursor=6

The same output on Textual 8.2.7 and on main at 5e5b7ef.

_Template.insert_text_at_cursor and _Template.delete_at_position both assert that the cursor isn't on a separator, but several things can leave it there:

  • the cursor at the end of a value that stops just before a separator (as above, or after setting value in code);
  • a click past the end of such a value (_on_click moves right off a separator, but the cursor is then clamped back to the end of the value);
  • home on a template that starts with a separator: it moves to 0, which is the separator. TomJGooding mentioned this one in the discussion on #6287.

I think the simplest fix is for those two methods to step over separators at the cursor instead of asserting. Typing then fills in the separator and goes on, as it does after the last character before a separator, and delete removes the next editable character, or does nothing at the end of the value. I'm happy to send a PR with tests.

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.