Fix annotations for pynput event callbacks#15857
Conversation
They are designed to accept any number of arguments up to the maximum amount, so `on_click()` and `on_click(x, y)` are both perfectly valid callbacks. Here are the full signatures for each callback: - `on_move(x: int, y: int, injected: bool)` - `on_click(x: int, y: int, button: Button, pressed: bool, injected: bool)` - `on_scroll(x: int, y: int, dx: int, dy: int, injected: bool)` - `on_press(key: Key | KeyCode | None, injected: bool)` - `on_release(key: Key | KeyCode | None, injected: bool)` Test passes with `python tests/regr_test.py pynput`
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Not a full review, just two things I noticed:
- We only need/want tests for particular tricky typing situations (for various reasons). In this case, the annotations are a bit unusual, but not particularly tricky, so the tests are not needed.
- I know this was a pre-existing problem, but callbacks returning
Noneis usually not correct. If the return value of the callback is ignored, we useobjectinstead.
(Also I respect the voodoo magic going on that supports callbacks with varying amounts of arguments.)
|
Alright cheers, I'll double check the return type and commit those changes then |
Only needed for complex cases, which this is not.
This comment has been minimized.
This comment has been minimized.
|
Awkward one with the return value actually. If it's specifically Not actually sure if that's still best left to def wrapper(f):
def inner(*args):
if f(*args) is False:
raise self.StopException()
return inner |
|
Interesting. Maybe using |
Technically they accept any return type, but as the only effect is returning False stops the iteration, then it makes sense to limit it to bool or None.
|
Yeah that makes sense I reckon - I also realised there was a mismatch with the keyboard ones so I've just updated them to match. |
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
The event callbacks are designed to accept any number of arguments up to the maximum amount. Also, currently, the
injectedargument is omitted from all the pyi files, so it'll flag up with an error if you use it.For example,
on_click(),on_click(x, y)andon_click(x, y, button, pressed, injected)are all perfectly valid callbacks.I've done a union with all possible signatures for the event callbacks, it's a little more verbose, but I'm not aware of another way to handle this.
For reference, here are the full signatures for each callback:
on_move(x: int, y: int, injected: bool)on_click(x: int, y: int, button: Button, pressed: bool, injected: bool)on_scroll(x: int, y: int, dx: int, dy: int, injected: bool)on_press(key: Key | KeyCode | None, injected: bool)on_release(key: Key | KeyCode | None, injected: bool)The test passes when running
python tests/regr_test.py pynput. Fixes #15855.