Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assigning a message type to a class variable breaks message handlers #3939

Open
arvidfm opened this issue Jan 1, 2024 · 1 comment · May be fixed by #3940
Open

Assigning a message type to a class variable breaks message handlers #3939

arvidfm opened this issue Jan 1, 2024 · 1 comment · May be fixed by #3940

Comments

@arvidfm
Copy link

arvidfm commented Jan 1, 2024

If you assign a message type to a class variable belonging to a class that derives from MessagePump, non-decorated message handlers stop working because the message type's handler_name incorrectly gets reassigned to use the name of the class containing the class variable as the namespace.

I ran into this when writing a general class for input prompts/modals, where I was using class variables to specify what event to treat as the "submit" event.

Simple example, where on_button_pressed won't be run unless EVENT_TYPE is removed:

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


class MyApp(App):
    # prevents on_button_pressed from running
    EVENT_TYPE = Button.Pressed

    def compose(self) -> ComposeResult:
        yield Button("Click me")

    def on_button_pressed(self) -> None:
        self.query_one(Button).label = "Clicked!"


if __name__ == '__main__':
    MyApp().run()
    # prints "on_my_app_pressed"!
    print(Button.Pressed.handler_name)
Copy link

github-actions bot commented Jan 1, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant