Hacker News new | past | comments | ask | show | jobs | submit login

Permitting arbitrary annotations is a rather nice feature that, besides static analysis, opens the door for expressive runtime pre/postcondition checking functionality like the Clojure spec library[1]. Does Python have something similar?

[1] https://clojure.org/guides/spec

Unfortunately PEP 563 [1] means that any non-type-hint usage of annotations is considered deprecated. You could still do things with decorators (like mentioned in a sibling comment), although I suppose that doesn't offer such an elegant syntax.

[1]: https://www.python.org/dev/peps/pep-0563/#non-typing-usage-o...

Although not really what they were intended for, somebody used annotations to provide a macro system [0].

[0] https://tinkering.xyz/abusing-type-annotations/

Author here. Be warned, this was just a proof of concept that I did because I thought it would be fun/weird/interesting!

Oh my goodness. I've committed some naughty deeds with the ast module before, but this is truly, wonderfully, heinous. You push this idea so far that I wouldn't even recognize some of these examples as syntactically valid, let alone predict their effects. I'm awestruck and a little frightened of you.

https://andreacensi.github.io/contracts/tour.html#quick-tour looks to be a stable solution for that.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact