Python3 annotations

As the commits will testify, this is not my work, I'm only the janitor here :-)

I've brought in @jlowin's Python 3 annotations work, merged some conflicts, and re-added some code that got dropped during the merge.
The docstring should probably stay on the main function.