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

Add type hints #505

Open
Avasam opened this issue Nov 16, 2021 · 4 comments
Open

Add type hints #505

Avasam opened this issue Nov 16, 2021 · 4 comments

Comments

@Avasam
Copy link
Contributor

Avasam commented Nov 16, 2021

Please add type hints to the functions

image

@boppreh
Copy link
Owner

boppreh commented Nov 18, 2021

Unfortunately one of the selling points of the library is compatibility with older Python versions, often necessary in some enterprise environments. Right now the library supports even Python 2.7, and type hints would eliminate that feature.

I'll continue monitoring community usage, and if Python 2-3.4 support is not necessary anymore, I'll drop it and add type hints.

Edit: I guess I could add at least a stub file for MyPy without breaking compatibility. I'll look into it.

@Avasam
Copy link
Contributor Author

Avasam commented Nov 19, 2021

.pyi files (type stubs) are the way to go in this case. Here's auto generated ones by Pylance. I've already filled in some that I use, namely send(), hook_key(), read_event() , KeyboardEvent (partial attributes), key_to_scan_codes() (partial return type) : keyboard.zip
Or the following typings folder which I'm updating as I'm using it: https://github.com/Toufool/Auto-Split/tree/2.0.0/typings/keyboard

@Avasam
Copy link
Contributor Author

Avasam commented Sep 2, 2022

Right now the library supports even Python 2.7, and type hints would eliminate that feature.

@boppreh You can have inline typing in Python2 code by using type comments https://mypy.readthedocs.io/en/stable/python2.html
Or by making stub files, but inline types have the advantage that they can be inferred (so you don't need to precisely type everything), and you can then typecheck the code using mypy and/or Pyright.

@Avasam
Copy link
Contributor Author

Avasam commented Sep 8, 2022

3rd party keyboard type stubs (by myself) have been added to typeshed python/typeshed#8666
Of course, this will only support the version available on PyPI, which is now far behind master.

It'd be best if the stubs were part of keyboard itself. You should be able to use them directly from typeshed (and they should be fairly accurate). Although they also wouldn't be removed from typeshed until a PyPI release of keyboard includes the stubs.

Ultimately, the best scenario is to have inline typing directly, as it reduces chances of desync between stubs and implementation, and allows for easy type-checking in the CI and during development. However, due to the Python2 support requirement, inline type comments may be annoying, and will be more complicated to support multiple Python version while trying to keep typing as accurate as possible depending on what said version supports.

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

No branches or pull requests

2 participants