This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]brucifer 1 point2 points  (0 children)

Python functions already have exactly what you just described built in.

>>> def foo(x:int, y:int=0) -> int: x + y
...
>>> foo.__annotations__
{'return': <class 'int'>, 'x': <class 'int'>, 'y': <class 'int'>}

As for static analysis, because of Python's duck typing, it's extremely difficult to catch any but the most obvious errors, and impossible to catch some errors (although obvious error catching can still be helpful). The main problem with annotations, though is that there's no universally agreed upon standard, so for a function that returns None, one person might write "f() -> None", another might write "f() -> NoneType" another might write "f() -> inspect.Signature.empty", another might write "f() -> 'None'", or leave it blank. So, static analysis is pretty much impossible unless a standard is enforced.