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

[Typing] Add type hint to sender in widget messages? #1813

Closed
rodrigogiraoserrao opened this issue Feb 16, 2023 · 8 comments
Closed

[Typing] Add type hint to sender in widget messages? #1813

rodrigogiraoserrao opened this issue Feb 16, 2023 · 8 comments
Labels
enhancement New feature or request Task

Comments

@rodrigogiraoserrao
Copy link
Contributor

The class Message defines the attribute sender as being of the type MessageTarget, which is a fairly generic protocol. Fine.

When writing custom messages, wouldn't it make sense to add an explicit type hint to mark the class variable sender as being of the type of the widget within which the message is being defined?

E.g., won't this make sense:

class MyWidget(Widget):
   class MyMessage(Message):

        sender: MyWidget  # Isn't this something sensible to do?

        def __init__(self, sender: MyWidget, ...):
            ...

By adding sender: MyWidget in the body of the class, IDEs and static checkers will let me make use of .sender as of being the type I already know it is.

@rodrigogiraoserrao rodrigogiraoserrao added the question Further information is requested label Feb 16, 2023
@willmcgugan
Copy link
Collaborator

Great in principle. But I suspect that will break typing somewhere. Feels like a LSP violation.

@davep
Copy link
Contributor

davep commented Feb 16, 2023

Perhaps the way to go here would be to make Message a generic, so the sub-class can be declared including the type expected for the sender?

@rodrigogiraoserrao
Copy link
Contributor Author

Assuming LSP means Liskov's Substitution Principle, I can't see how that could be true.
A message is defined inside a widget namespace, implying that the sender will be an instance of that type of widget or a subclass of another. If B is a subclass of A and the sender is typed as A, it is fine if the message is sent from B.

@rodrigogiraoserrao
Copy link
Contributor Author

Reading this on the mypy docs makes me wonder whether I'm getting things the other way around.
I'll wait for a eureka moment or for someone to enlighten me.

@rodrigogiraoserrao
Copy link
Contributor Author

Work on #1831 seems to suggest there is no issue with LSP.
I request we change this into an enhancement task and add it to the backlog.

@willmcgugan
Copy link
Collaborator

Ok, feel free to add that.

@rodrigogiraoserrao rodrigogiraoserrao added enhancement New feature or request Task and removed question Further information is requested labels Feb 27, 2023
@rodrigogiraoserrao
Copy link
Contributor Author

rodrigogiraoserrao commented Mar 6, 2023

This became irrelevant as of #1940.

@github-actions
Copy link

github-actions bot commented Mar 6, 2023

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants