`MaskedInput` crashes with `AssertionError` when you type or delete with the cursor on a separator
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 77/100
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
valuein code); - a click past the end of such a value (
_on_clickmoves 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
- 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