5 ms·
Yeah, I'm pretty sure Python itself explicitly uses the term "annotations" everywhere instead of "type hints". The point is that they can be used for anything,
by ehsankia 7y ago
Yeah, I'm pretty sure Python itself explicitly uses the term "annotations" everywhere instead of "type hints". The point is that they can be used for anything, typing being one such use.
As for libraries, other than MyPy, there's also Pyre by Facebook and Pytype by Google. I'm definitely excited to see more advanced static analysis being implemented beyond simple type checking. Some of these libraries are starting to explore that.
- Sharlin 7y agoPermitting 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 https://clojure.org/guides/spec
- jacobush 7y agohttps://andreacensi.github.io/contracts/tour.html#quick-tour https://andreacensi.github.io/contracts/tour.html#quick-tour looks to be a stable solution for that.
- auscompgeek 7y agoUnfortunately 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-of-annotations https://www.python.org/dev/peps/pep-0563/#non-typing-usage-o...
- DonaldPShimoda 7y agoAlthough not really what they were intended for, somebody used annotations to provide a macro system [0]. [0] https://tinkering.xyz/abusing-type-annotations/ https://tinkering.xyz/abusing-type-annotations/
- z_mitchell 7y agoAuthor here. Be warned, this was just a proof of concept that I did because I thought it would be fun/weird/interesting!
- neoncontrails 7y agoOh 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.
- ieie92ndjrj 7y agoThe docs at python.org refer to them as “type hints” ️ https://docs.python.org/3/library/typing.html https://docs.python.org/3/library/typing.html
- uranusjr 7y ago`typing` is the module that uses annotations as type hints. Parent is correct; Python documentation calls the syntax annotations.
- ieie92ndjrj 7y agoSo Python docs refer to both depending on the context.
- ehsankia 7y agoAs mentioned in the other comment, this is just a library used to give type hints as annotations. The feature itself is called annotations, this is one specific use-case. The reason this library exists in stdlib is so that all the different type hint libraries can interoperate. Otherwise if they each brought their own typing, you couldn't switch between different static analysis libraries.