Skip to content

Button Action Parameter Not Detecting In Correct Namespace #5394

Open
@ddkasa

Description

@ddkasa

So it says this here

Textual will look for action methods in the class where they are defined (App, Screen, or Widget). If we were to create a custom widget it can have its own set of actions.

But after running the provided MRE no action gets detected for the buttons. Although if I set the namespace explicitly the action still works.

self.call_later(
self.app.run_action, self.action, default_namespace=self._parent
)

My suspicion is that the above default_namespace parameter is the culprit, as its pointing to the incorrect parent Screen(id="_default"), which doesn't contain the method. For example when I change it to self.app, as a band aid fix it works correctly. In my opinion it should search up the DOM to the active screen, until an action actually matches or if no explicit screen is set it should check the main app class last instead of a screen.

MRE

from textual.app import App
from textual.widgets import Button


class BugReportApp(App):
    BINDINGS = [("n", "notify_test")]

    def compose(self):
        yield Button("Broken", action="notify_test")
        yield Button("Works", action="app.notify_test")

    def _action_notify_test(self):
        self.notify("It works")


if __name__ == "__main__":
    BugReportApp().run()

Textual Diagnostics

Versions

Name Value
Textual 1.0.0
Rich 13.9.4

Python

Name Value
Version 3.12.3
Implementation CPython
Compiler GCC 13.2.1 20240316 (Red Hat 13.2.1-7)
Executable /home/dk/dev/tmp/t-button-action/.venv/bin/python

Operating System

Name Value
System Linux
Release 6.11.11-200.fc40.x86_64
Version #1 SMP PREEMPT_DYNAMIC Thu Dec 5 18:38:39 UTC 2024

Terminal

Name Value
Terminal Application Kitty
TERM xterm-kitty
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=137, height=54
legacy_windows False
min_width 1
max_width 137
is_terminal True
encoding utf-8
max_height 54
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