Textualize/textual · error · InvalidSelectValueError

Can't clear selection if allow_blank is set to False.

Error message

Can't clear selection if allow_blank is set to False.

What it means

Raised by Select.clear() when allow_blank is False. clear() assigns the internal NULL sentinel value; the value validator rejects it because blank is not among the legal values, and the error is re-raised with a clearer message.

Source

Thrown at src/textual/widgets/_select.py:705

    def is_blank(self) -> bool:
        """Indicates whether this `Select` is blank or not.

        Returns:
            True if the selection is blank, False otherwise.
        """
        return self.value == self.NULL

    def clear(self) -> None:
        """Clear the selection if `allow_blank` is `True`.

        Raises:
            InvalidSelectValueError: If `allow_blank` is set to `False`.
        """
        try:
            self.value = self.NULL
        except InvalidSelectValueError:
            raise InvalidSelectValueError(
                "Can't clear selection if allow_blank is set to False."
            ) from None

    def _watch_prompt(self, prompt: str) -> None:
        if not self.is_mounted:
            return
        select_current = self.query_one(SelectCurrent)
        select_current.placeholder = prompt
        if not self._allow_blank:
            return
        if self.value == self.NULL:
            select_current.update(self.NULL)
        option_list = self.query_one(SelectOverlay)
        option_list.replace_option_prompt_at_index(0, Text(prompt, style="dim"))

View on GitHub (pinned to 06dbeef4bb)

Solutions

  1. Construct the Select with allow_blank=True if you need to clear it.
  2. Guard: only call clear() when select.allow_blank.
  3. Reset to a default option value instead of clearing: select.value = default_value.

Example fix

# before
Select(options, allow_blank=False)
# ...
select.clear()  # raises
# after
Select(options, allow_blank=True)
# ...
select.clear()
Defensive patterns

Strategy: validation

Validate before calling

if select.allow_blank:
    select.clear()
else:
    select.value = first_option_value

Try / catch

try:
    select.clear()
except InvalidSelectValueError:
    pass  # allow_blank is False

Prevention

When it happens

Trigger: Creating Select([...], allow_blank=False) and later calling select.clear(), often in a form-reset handler.

Common situations: Form reset buttons, clearing selection after data reload, or generic reset code that assumes every Select can be cleared.

Related errors


AI-assisted analysis of Textualize/textual@06dbeef4bb (2026-08-27). Data as JSON: /api/errors/45c820d63c76381b. Report an issue: GitHub.