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

Open Beginner friendly
#6,761 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
77/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
cli, frontend

Research direction

Read MaskedInput and its _Template.insert_text_at_cursor and _Template.delete_at_position methods, along with the existing widget tests. Reproduce the crash using run_test() and pilot.press() with the provided mask and value. Done when typing and deleting at separator positions no longer assert and the described expected behavior is covered by tests.

Written by the indexing model from the issue text.

Description

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.

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.