Skip to content

Crash on first Input in compose of app.DEFAULT_MODE screen #6444

@cognifloyd

Description

@cognifloyd

The bug

Initially reported on discord: https://discord.com/channels/1026214085173461072/1033754296224841768/1483472126802661407

I ran into an issue when I tried to add the first Input.

It seems Input cannot be used with app.MODES, or at least not in the compose method of the first screen of the app.DEFAULT_MODE (I did not set app.SCREENS). That kills the app with a traceback for this error:

ScreenStackError: No screens on stack

The crash happens because Inputs watchers end up trying to do self.app.clear_selection().

def _watch_selection(self, selection: Selection) -> None:
self.app.clear_selection()

That has this try/except:

textual/src/textual/app.py

Lines 4555 to 4560 in 0f0849f

def clear_selection(self) -> None:
"""Clear text selection on the active screen."""
try:
self.screen.clear_selection()
except NoScreen:
pass

That only catches NoScreen, but with modes a ScreenStackError is raised because that first screen hasn't been mounted yet, so the stack is still empty.

For now, I'm working around the issue by adding a clear_selection method to my app that does:

def clear_selection(self):
    try:
        super().clear_selection()
    except ScreenStackError:
        pass

This workaround is based on several places in the code base that do except NoScreen, ScreenStackError, so it seemed like a reasonable fix.

On discord, David said the following:
https://discord.com/channels/1026214085173461072/1033754296224841768/1483494995523534999

Interesting. I can reproduce this using the modes01.py example from the Guide (and replacing the dashboard Placeholder by an Input). HOWEVER, if you do not use DEFAULT_MODE but use the code in the modes01.py example, you don't run into this problem. So do this:

    # DEFAULT_MODE = "dashboard"

    def on_mount(self) -> None:
        self.switch_mode("dashboard")

(change the name of course) and skip using DEFAULT_MODE, at least for now.

Textual Diagnostics

Versions

Name Value
Textual 8.1.1
Rich 14.3.2

Python

Name Value
Version 3.13.7
Implementation CPython
Compiler Clang 20.1.4
Executable /home/jafloyd/g/github/copart-pe/ghe.git/.venv/bin/python3

Operating System

Name Value
System Linux
Release 6.8.0-90-generic
Version #91~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov 20 15:20:45 UTC 2

Terminal

Name Value
Terminal Application Konsole 220403
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=236, height=55
legacy_windows False
min_width 1
max_width 236
is_terminal True
encoding utf-8
max_height 55
justify None
overflow None
no_wrap False
highlight None
markup None
height None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions