`MaskedInput` validation passes with required `X` places left empty

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

Research direction

Start from MaskedInput and its _Template.validate, which the issue names directly; reproduce with the three-widget snippet to confirm all three cases should be invalid. Compare how padding with chr(0) lets [^ ] matches through at the end of the value, while a space in the middle fails, and check the existing masked-input tests for the expected validation behavior. Done means empty required X places fail at the end as well as in the middle, with a regression test covering both the all-X and mixed templates.

Written by the indexing model from the issue text.

Description

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

The docs list X ([^ ]) as a required template character. But a MaskedInput validates as valid when its X places at the end are empty, and so does one whose template is only X places with no value at all. A 9 place in the same position is reported as missing, as expected.

import asyncio

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

class MaskedApp(App):
    def compose(self) -> ComposeResult:
        yield MaskedInput("XXX", id="code")
        yield MaskedInput("99-XX", value="12-", id="mixed")
        yield MaskedInput("99-99", value="12-", id="digits")

async def main():
    app = MaskedApp()
    async with app.run_test():
        for masked in app.query(MaskedInput):
            result = masked.validate(masked.value)
            print(
                f"template={masked.template!r} value={masked.value!r}: "
                f"is_valid={result.is_valid}"
            )

asyncio.run(main())

Output on Textual 8.2.7, and the same on main at 5e5b7ef:

template='XXX' value='': is_valid=True
template='99-XX' value='12-': is_valid=True
template='99-99' value='12-': is_valid=False

Expected: False for all three. An empty X place in the middle of the value (12- 4 on 99-XX) already fails, because it holds a space.

_Template.validate pads the value to the template's length with chr(0) before checking it, and [^ ] matches chr(0). So padded X places pass, while every other required pattern rejects chr(0). Padding with spaces, which is what an empty place holds inside the value, would make the end of the value behave like the middle. I'm happy to send a PR.

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.