-
Notifications
You must be signed in to change notification settings - Fork 704
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 #162
Comments
I actually never used type hints. What benefits do you foresee if those are added? |
Type hints may serve as in-code documentation and help with early bug detection, assuming your IDE supports it. Take for example the following snippet: def circumference(radius: float) -> int:
return round(2 * radius * 3.14)
radius = input("Radius: ")
result = circumference(radius)
ratio = result.as_integer_ratio() Right off the bat I can see the types of the argument and return value I can expect from this function, which may not be obvious at first if you're not familiar with the
This is particularly useful when using third party modules for which you don't want to read the documentation or understand the code. As for Loguru, having such a simple API, there is not much that can go wrong. But I still see some benefits for things like the type of the |
Thanks for the detailed explanations and code sample! It seems it can indeed be useful. Anyway, as you said it looks better than docstrings to properly define function signatures. I will try to add this to the public methods of |
A stub file is preferred over inline type hints for several reasons: - it avoids performance penalty, especially for annotating ".add()" which would require importing "logging" (while it could be imported lazily by the implementation while evaluating "sink") - it reduces readability while calling "help()", it does not seem to add much compared to docstrings and is redundant - it supports latest features like forward references natively - it prevents cluttering the source code with all custom types - it works much better with some features like "@overload" - it is required to annotate the (future) C implementation
So, it was for me the opportunity to discover Python type hints. I implemented types in a separate I did my best to annotate Loguru's public interface but there may be problems I overlooked. Please let me know if incorrect warnings are reported by your type checker. I hope this will help tools like IntelliSense auto-completion for example. 👍 |
Loguru could use some type hints. Is that on the roadmap yet?
The text was updated successfully, but these errors were encountered: