Python Forum

Full Version: Type Hinting - who's using it?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Who's using type hinting? What are your thoughts on it so far?
I wasn't aware it was there.
May (possible) use going forward can report back after I read up (later today)
i guess i just got use to programming in python without it so i never use it.
Have look into it and used it some times more to test it out.
Can use it there it make sense trough the concept of gradual typing.
Code without type hints will be ignored by the static type checker.
Therefore can start adding types to where it make sense and critical components,and continue as long as it adds value to you.

Bigger companies like eg Dropbox and Instagram use this heavily think now is almost mandatory for them.
Our journey to type checking 4 million lines of Python
Instagram has made MonkeyType to add type annotations to code where is not automatically.
So can take MonkeyType Shifty for test to see what it dos.
def double(number):
    return number * 2.5

print(double(4))
print(double(5.5))
Output:
10.0 13.75
So can take integer or float as input,and output will be float.
Using MonkeyType:
E:\div_code\read
λ monkeytype apply clean
After apply:
from typing import Union

def double(number: Union[float, int]) -> float:
    return number * 2.5

print(double(4))
print(double(5.5))

Conclusion i quote Python Type Checking (Guide) it's a well written Guide bye Geir Arne Hjelle a fellow countryman.
Geir Arne Hjelle Wrote:Conclusion

Type hinting in Python is a very useful feature that you can happily live without.
Type hints don’t make you capable of writing any code you can’t write without using type hints.
Instead, using type hints makes it easier for you to reason about code, find subtle bugs, and maintain a clean architecture.
I don't use it when doing quick prototyping, but I include it in all code at work, since the people I work with are used to languages like Scala and aren't used to dynamic typing. I agree that it doesn't let you write code you couldn't otherwise, but it really helps for things like code review where you don't have Pycharm or anything like that to help you figure out what types are. I've even caught would-be type errors in code review because people annotated their code correctly but forgot to run mypy to detect the contradiction.
I can see where it would come in handy. When debugging, I often have print statements like print(f"{type(variable)}")
I don't use it.
My scripts usually aren't very big. And I prety much remember what I wrote.
TypeHints helps me to recognize the API.

I don't use them in short scripts, but if the code is on different modules, I use them.
PyCharm will detect the use of TypeHints and will warn you, if you have chosen the wrong type.