Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
type annotations
#1
I have occasionally used type hinting and mypy.

Objectively I understand the benefits but subjectively it looks too javasque for me to really like it. As I generally try to avoid things that I don't like I may unintentionally make my life harder.

So, in order to convince my inner me: do you use it and what is your experience? What are the (subjective) scenarios/examples/reasons where it really shines?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#2
I just use it without mypy. TypeHintes helps also the IDE to detect wrong datatypes during development.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
I don't bother with them for prototyping, but I'm a fan of them for anything non-trivial, especially if someone else might read that code someday (e.g. me in a few months). I was code reviewing some Python the other day and I spent far too much time trying to figure out the types, partly because they had a variable which could be a string or a dictionary (erroneously). If they'd have spent a negligible amount of time adding type hints to their code, I would have spotted the bug much, much faster, assuming they didn't find it themselves. I have in my queue of things to talk to my manager about to require type hinting in Python written at my company. We're primarily a Scala shop, and Scala has a very robust static typing system, so I expect it'll be well-received.

(Imagine how much easier it would be reading huge blocks of code on the forum if they're type-hinted and you know mypy passes. I'm probably still too lazy to read code long enough for it to be worth it, but hey.)
Reply
#4
I have looked into it,not used so much in code yet.
I like 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.

A quick look.
from typing import Union

def search_for(needle: str, haystack: str) -> Union[int, None]:
    offset = haystack.find(needle)
    if offset == -1:
        return None
    else:
        return offset

print(search_for('s', "my string"))
So it easy to see that argument to function is strings,and return value from function is integer or None.

mypy on commanline and in VS Code selected as linter.
Wrong input no difference running if with Python(run to TypeError),but mypy will detect it earlier and editors like eg VS Code and PyCharm has detection in editor.
print(search_for(5, "my string"))
[Image: hC9h9G.png]
[Image: QAW83o.png]

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) which is written bye a fellow countryman Geir Arne Hjelle.
It's a well written Guide.
Quote: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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020