Skip to the content.

G+: Python as a statically typed language? Yup, it's …

David Coles
Python as a statically typed language? Yup, it's possible.

One of the lesser known features that was added in Python 3 was the ability to specify annotations for function arguments and return values. Unfortunately one thing it didn't specify was the form of these annotations.

Recently, JetBrains (the developers of the PyCharm IDE) and Jukka Lehtosalo have been collaborating on a type annotation system that can be used to perform robust static analysis on Python code.

The annotations are very similar to the kind that many developers already do in Python docstrings and can also be used for type hinting (see http://www.jetbrains.com/pycharm/webhelp/type-hinting-in-pycharm.html for a good example). For IDE developers it makes a huge amount of sense - without annotations providing meaningful Intelligent Code Completion (a.k.a IntelliSense) is very hit and miss.

Also take a look at http://blog.pirx.ru/media/files/2013/python-optional-typing/ for a good background into why you would want static typing and some other developments in that area.

mypy - Optional Static Typing for Python


(+1's) 1
Tim Cuthbertson
I've used TypeScript a bit, which sounds pretty similar (but for JS). It is useful, but not as much as you'd hope. In a statically typed language you have a pretty good idea of what the compiler is proving for you, and when you're flouting its rules (e.g. casting). But a number of times with TypeScript I'd find sneaky bugs where it looked like the code must be doing the right thing (the kind of issues a compiler would definitely tell you about, like the wrong argument types). Only to find out the types were wrong, but the compiler ignored it because it got tainted by some small omission or interaction with untyped code.

David Coles
I'm kind of curious to see how it ends up working. One of the big motivations is improving code completion quality in IDEs (a few coworkers fans of PTVS and I like some of PyCharm's features). Even just working as a function parameter type-linter would be handy.

As for using static typing all the way through... will have to try it out and see what sort of value there is over the extra development effort.

One thing I do like is Protocols, which I think beats the hell out of using ABCMeta. I'm told it's very similar to Interfaces in Go or TypeScript. For me they'd be a nice way of formalizing contracts in a way that a tool could verify (as opposed to just throwing more documentation at it)

 

Tim Cuthbertson
Ahh yep, those both sound pretty useful. And at least in the IDE case, you notice when it fails to type something properly, because you get crappy (or missing) suggestions ;)

David Coles
Just fill your code with assert(typeof(x, Foo)) until the IDE gets the drift. ;)

Actually, showing what the IDE has seen would be interesting. Or maybe filtering down types based on how you use it (you used send, probably typed as Sendable, socket.socket, ...)