On Friday, January 8, 2016 at 3:06:05 PM UTC-8, Guido van Rossum wrote:
At Dropbox we're trying to be good citizens and we're working towards introducing gradual typing (PEP 484) into our Python code bases (several million lines of code). However, that code base is mostly still Python 2.7 and we believe that we should introduce gradual typing first and start working on conversion to Python 3 second (since having static types in the code can help a big refactoring like that).
Since Python 2 doesn't support function annotations we've had to look for alternatives. We considered stub files, a magic codec, docstrings, and additional `# type:` comments. In the end we decided that `# type:` comments are the most robust approach.
FWIW, we had the same problem at Google. (Almost) all our code is Python 2. However, we went the route of backporting the type annotations grammar from Python 3. We now run a custom Python 2 that knows about PEP 3107. The primary reasons are aesthetic - PEP 484 syntax is already a bit hard on the eyes (capitalized container names, square brackets, quoting, ...) , and squeezing it all into comments wouldn't have helped matters, and would have hindered adoption. We're still happy with our decision of running a custom Python 2, but your mileage might vary. It's certainly true that other tools (pylint etc.) need to learn to not be confused by the "odd" Python 2 syntax. [1] I have a prototype of such a tool, i mplemented as a 2to3 fixer. It's a
bit over 200 lines. It's not very interesting yet, since it sets the types of nearly all arguments to 'Any'. We're considering building a much more advanced version that tries to guess much better argument types using some form of whole-program analysis. I've heard that Facebook's Hack project got a lot of mileage out of such a tool. I don't yet know how to write it yet -- possibly we could use a variant of mypy's type inference engine, or alternatively we might be able to use something like Jedi ( https://github.com/davidhalter/jedi).
pytype (http://github.com/google/pytype) already does (context sensitive, path-sensitive) whole-program analysis, and we're working on making it (more) PEP 484 compatible. We're also writing a (2to3 based) tool for inserting the derived tools back into the source code. Should we join forces? Matthias