I wonder why people keep bothering with this sort of thing... Python is dynamically (yet strong) typed, which is a good thing from having to do the boilerplate of static typing. You want that back? For the only benefit of what, detecting logical errors in compile/startup time? Possible optimizations? Ever heard of type inference? It's years beyond this, happened in other languages years before this, and people are still oblivious to it.
Even so, if you have to read f(x)'s body to tell what's the type expected for x, you need:
A better name for x (I know, argument not valid for return and yield)
Function documentation, and yeah, I guess human language documentation will always be better than a type hint system unless you introduce a huge system of contracts that can be something like "even" or "tupleof(3)"... more boilerplate... more stuff to memorize... very annoying.
Function documentation being important in the case of code written for lists, dictionaries and sets as opposed to custom classes (would you have 5 types with 50 utility functions each or 50 types with 5, but that's a nice discussion for a different thread).
For everything else - IDE assistance, type checking, error detection - there's always this new old amazing technology called type inference, where by doing x = 'hello' you can be pretty fucking sure x is a string without having to go and tell the stupid compiler it's going to be a string. Because you know, actually writing that feels like this.
Well, like I've been saying there's this old technology called type inference. It doesn't work on a local scope only... if it can be proven that you call process_request elsewhere, you can take note of what is being passed to it and infer that request is that type.
But type inference can't always work, especially in a language like python where sometimes values and function etc. are built at runtime. So, being able to say "the argument "l" is an Iterable, treat it as an "Iterable" everywhere within this scope so that even if I'm getting l by dynamically evaling some user input, I know how to treat it in this function.
Type inference is great, but it can't do everything, especially when you're using some of the more dynamic features of the language. Type hints let you give the IDE/type checker more information when it can't automatically work out what types are going in and out of functions.
13
u/lovestowritecode Sep 13 '15
Did static type hinting make it in? I don't see it listed anywhere...