textual
textual copied to clipboard
Button Action Parameter Not Detecting In Correct Namespace
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.
https://github.com/Textualize/textual/blob/3f3259246f4cb8956d1818b1c516c23e7777f606/src/textual/widgets/_button.py#L287-L289
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 |
We found the following entry in the FAQ which you may find helpful:
Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.
This is an automated reply, generated by FAQtory
This was changed way back in https://github.com/Textualize/textual/releases/tag/v0.61.0 to make it a requirement that the namespace is specified. I think that's just some outdated documentation.
Changed in PR: https://github.com/Textualize/textual/pull/4516
I can create a PR adding a sentence specifying that a namespace is required if using the action parameter.
The MRE for this example doesn't seem quite (See #5422). I wonder if that is relevant to this issue.