2021-05-20
The temptation to start using some Python type hints
I've been hearing about Python 3's optional type hints for some time now (which is not surprising, since they were introduced in Python 3.5, released in 2015, although then improved in several subsequent releases), but for a long time I didn't really pay much attention. However, lately I've been feeling a temptation to try them out in some of my programs, assuming that I can find a suitable one.
One significant driver of this new temptation has been using GNU Emacs with LSP based editing for both Python and Go. My experience has been that LSP-based editing is significantly nicer for my Go than for my Python, partly because I get much better completion suggestions for Go. I suspect that having typing information would improve things here when I'm editing Python.
(Reading around a bit, it appears that pyls has more or less native support for type hints through using Jedi, with optional use of mypy through pyls-mypy. Also, apparently I probably want to switch to python-lsp-server instead of pyls, cf.)
Another reason is particular to my circumstances with Python. I deal with a lot of Python code that I touch only infrequently, when it needs some new feature or a bug fix; most of the time it sits there working away. When it's been at least months since I last looked at the code, going back to it always involves some amount of re-discovery of what functions do, how things flow in the code, and what types various things are. Adding type hints would help speed up that re-learning of my code, and as a bonus it would help catch any typing mistakes that I make as I do my additions, changes, or bug fixes.
(I've definitely observed this advantage when I go back to my Go code, and having enforced types in Go gives me more confidence in changes. To get something like this in Python, I assume I would want to use mypy.)
For various reasons, I'd want to start with my own personal code instead of initially adding any type annotations to our production code. Type hints are still unusual Python, and until they prove their value to us I need to be conservative in that sort of thing. It is tempting to add some type annotations to a private copy of some of our production code, just to see if mypy reports any type confusions or conflicts that turn up bugs.
PS: I expect type hints to work relatively well for my code because
it tends to be more or less statically typed in practice at runtime.
Where they're not strictly statically typed, I could probably mostly
deal with it with things like typing.Union
and
typing.Optional
.