In Python 4.0 (the one after 3.9), and in Python 3.7 now with "from __future__ import annotations", annotations are parsed but their interpolation is delayed. So you can literally have "var: 0 < var < 3" (annotation is not a string!) in your code and make it work as you wish.
Unchecked type "annotation" was one of the worst features ever added to a reasonably popular programming language. Python did OK without type declarations. Optional type declarations could help with checking, documentation, and optimization. What went in is the worst of both worlds. Type annotations don't do much, and they might be wrong.<p>Now this. Ugh. This is abusing an internal feature of one implementation.
I've been experimenting with annotations to chain a set of functions together using a common namespace. In a nutshell,<p><pre><code> ns.register(foo: int, bar: int)
@ns
def add_one(foo : ns.foo) -> ns.bar:
return foo + 1
</code></pre>
It gets a good bit more complicated than this, but when you've got a whole mess of functions to chain together, this lets you do it _reasonably_ sanely and figure out the ordering.<p>I think it should be possible to be compatible with a type checker like mypy as it could reannotate the decorated function with the types looked up from the namespace, but I haven't tested that.
I did something similar to the ending of this post, where the realization sets in that annotations are just expressions. I wanted to force the method's annotated parameters and return type before entering and exiting the method.<p><a href="https://gist.github.com/kryptn/2f91fdd9f4c5b2a272472d5e1a5afc5e" rel="nofollow">https://gist.github.com/kryptn/2f91fdd9f4c5b2a272472d5e1a5af...</a>