-
-
Notifications
You must be signed in to change notification settings - Fork 685
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
Add type annotations for callbacks #2042
Comments
Definitely agreed that we can do a lot better than Based on in-person discussion, it sounds like defining a Protocol might be the way to go, with the added benefit that this provides a class that we can use to attach documentation to so we can describe conventions like using |
What is the problem or limitation you are having?
toga.Button's
on_press
parameter is annotated ascallable|None
, but it calls this callable likef(widget)
, so the callable has to accept a parameter:Clicking the button leads to an error, because
say_hello
has an incompatible signature.Since
on_press
is not annotated as e.g.Callable[[Widget], None]
, static type checkers like mypy or PyCharm don't catch this error.Describe the solution you'd like
I'd like
on_pressed
to be annotated asCallable[[Widget], Any]
,Callable[[Button], None]
, or whatever fits best and prevents such errors (I'm not deeply familiar with toga semantics).Describe alternatives you've considered
One could also inspect the callable and only pass the widget as parameter if the signature expects a parameter.
This would also prevent runtime issues with callbacks not requiring a reference to the calling widget, which I think is the most common source for signature mismatches here.
However, a callback like
show_number_dialog(x: int) -> None
would still not be flagged by static type checking.Additional context
No response
The text was updated successfully, but these errors were encountered: