-
Notifications
You must be signed in to change notification settings - Fork 340
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
Please add type hints or type stubs #151
Comments
Can this be automatically generated, e.g., by setup.py? |
Also, please explain why this is needed. |
Type hints in Python are great for type safety. Python being a dynamically typed language, it's really easy to loose track of your variables' types, which leads to many runtime errors. There are two main ways to add type hints:
https://docs.python.org/3/library/typing.html The file I provided was generated by Pylance, but most everything is typed as Unknown and needs to be manually filled in. there's a limit to what any tool can guess you meant for any variable to be. |
Do the docstrings need to be in the stub? If something is left out in .pyi, what are the consequences? From what I see from the file, it could be possible to generate it with a small look up table (variable name -> type and function name -> return type). |
I would also like to +1 the niceness of type hints! I guess doing it in stub files means that'll all be fine though! |
I am wondering how to do this with the least amount of future maintenance effort. I am worried that the stub file may become outdated if not care is taken. That's why I am wondering if it could be automatically generated, by parsing (perhaps even just text replacement) and injecting the types as needed.
within the brackets, the arguments can be split, and replaced by name matching:
An alternative is to keep the type annotations in comments in the source files just above each function signatures, and then extract these with a script into a type stub. Another alternative is to use a program that can infer types. Perhaps https://google.github.io/pytype/ can do it? |
You don't need to do that, you can add the type annotations directly into the Python source code. Python's type annotations are designed for that, and they are being ignored by the normal interpreter, and only used by typecheckers such as I can very much recommend to use types, by the way. We found it made our Python code a lot more robust, and it makes libraries like this one easier to use for users (both when they are using a tool like I've made the file from #151 (comment) more complete for my |
@nh2, imagehash is trying to support python2, which as far as I understand does not ignore these type annotations, but I may be wrong. |
aha, there is a way: https://mypy.readthedocs.io/en/stable/python2.html |
Here's an autogenerated type stub. I've already filled in phash since that's the one thing I use:
imagehash/__init__.pyi
The text was updated successfully, but these errors were encountered: