moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.74k stars 244 forks source link

Support type annotations #549

Closed abrichr closed 1 year ago

abrichr commented 1 year ago

Description Adding types to callbacks produces the following exception:

Exception in thread Thread-5 (read_mouse_events):
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.10/3.10.11/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
2023-04-17 13:30:20.303 | INFO     | __main__:process_events:91 - starting
    self.run()
  File "/usr/local/Cellar/python@3.10/3.10.11/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/abrichr/MLDSAI/src/puterbot/puterbot/record.py", line 562, in read_mouse_events
    mouse_listener = mouse.Listener(
  File "/Users/abrichr/MLDSAI/src/puterbot/.venv/lib/python3.10/site-packages/pynput/mouse/_darwin.py", line 170, in __init__
    super(Listener, self).__init__(*args, **kwargs)
  File "/Users/abrichr/MLDSAI/src/puterbot/.venv/lib/python3.10/site-packages/pynput/mouse/_base.py", line 271, in __init__
    on_move=self._wrap(on_move, 3),
  File "/Users/abrichr/MLDSAI/src/puterbot/.venv/lib/python3.10/site-packages/pynput/_util/__init__.py", line 283, in _wrap
    argspec = inspect.getargspec(f)
  File "/usr/local/Cellar/python@3.10/3.10.11/Frameworks/Python.framework/Versions/3.10/lib/python3.10/inspect.py", line 1237, in getargspec
    raise ValueError("Function has keyword-only parameters or annotations"
ValueError: Function has keyword-only parameters or annotations, use inspect.signature() API which can support them

Platform and pynput version MacOS 13.2.1 (22D68) Pynput: https://github.com/abrichr/pynput (based on https://github.com/moses-palmer/pynput/commit/96d50cbae0e9cfb21f7e236cec8cb298a256c1f9)

To Reproduce

from pynput import mouse

def on_move(
    x: int,
    y: int,
) -> None:
    pass

def main():
    mouse_listener = mouse.Listener(
        on_move=on_move,
    )

if __name__ == "__main__":
    main()

Resolution

Change inspect.getargspec(f) to argspec = inspect.getfullargspec(f).

See https://github.com/abrichr/pynput/commit/5d7256761b3cf6f01758c2d7d6a877d8facf79bb

moses-palmer commented 1 year ago

Thank you for your report.

I have to admit to being slightly confused about this issue, as I could not find any use of inspect.getargspec in the code; I did not realise that this issue was on the feature branch feature-injected!

I have rebased the feature branch onto the current master. Please note that the branch is currently not slated for merging into master, as I have yet to find a way to make it work on Xorg.