I have never seen this feature used in the wild. However, one potential use of function annotations, which I studied in a Python 3 article that I wrote for USENIX; Login: to enforce contracts. For example, you can do this:
from functools import wraps def positive(x): 'must be positive' return x > 0 def negative(x): 'must be negative' return x < 0 def ensure(func): 'Decorator that enforces contracts on function arguments (if present)' return_check = func.__annotations__.get('return',None) arg_checks = [(name,func.__annotations__.get(name)) for name in func.__code__.co_varnames] @wraps(func) def assert_call(*args): for (name,check),value in zip(arg_checks,args): if check: assert check(value),"%s %s" % (name, check.__doc__) result = func(*args) if return_check: assert return_check(result),"return %s" % (return_check.__doc__) return result return assert_call
If you do this, you will see the following:
>>> foo(2,-3) 5 >>> foo(2,3) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "ensure.py", line 22, in assert_call assert check(value),"%s %s" % (name, check.__doc__) AssertionError: b must be negative
I should note that the above example should be more detailed in order to work correctly with default arguments, keyword arguments, and other details. This is just a sketch of an idea.
Now, whether this is really a good idea or not, I just don't know. I tend to agree with Brandon that the lack of compositional ability is a problem, especially if annotations start being used by different libraries for different purposes. I also wonder if it's possible to do something like this contract idea through decorators. For example, creating a decorator that was used in this way (the implementation is left as an exercise):
@ensure(a=positive,b=negative) def foo(a,b): return ab
Historical note. I have always believed that function annotations are the result of discussions about the “optional static typing” that the Python community had over 10 years ago. Whether this is an original motivation or not, I just don't know.