Proposals have turned into major language changes for Java lately, and Python is poised to follow suit.
Programmer Guido van Rossum, author of the Python programming language, published a proposal on the Python mailing list suggesting that Python should learn from languages such as Haskell and the experimental mypy Python variant’s syntax for function annotations. Mypy, a static Python type checker, finds type errors during compilation when using custom syntax to add type annotations, but the custom syntax is actually valid Python 3 code.
“The goal is to make it possible to add type checking annotations to third-party modules (and even to the stdlib) while allowing unaltered execution of the program by the (unmodified) Python 3.5 interpreter,” van Rossum wrote. “The actual type checker will not be integrated with the Python interpreter, and it will not be checked into the CPython repository.”
Van Rossum’s proposal listed several reasons why adding type annotations in Python is a good idea:
• Editors/IDEs: Type annotations would call out misspelled method names or inapplicable operations and suggest possible names.
• Linter capabilities: Mypy annotations work much like a linter, which finds certain types of code errors more quickly.
• Standard notation: Developing a standard notation for type annotations will reduce the amount of documentation that needs to be written. Once a standard type annotation syntax is introduced, van Rossum said it should be simple to add support for this notation to documentation generators like Sphinx.
• Refactoring: Type annotations help in manually refactoring code as well as certain automatic refactoring. “Imagine a tool like 2to3 (but used for some other transformation) augmented by type annotations, so it will know whether [for example] x.keys() is referring to the keys of a dictionary or not,” van Rossum wrote.
While explaining all the reasons why type annotations would be useful in Python, he made clear that the language will not technically be adopting standard rules for type checking.
“Fully specifying all the type-checking rules would make for a really long and boring PEP [Python Enhancement Proposal],” van Rossum wrote. “The worst that can happen is that you consider your code correct but mypy disagrees; your code will still run. That said, I don’t want to completely leave out any specification… Maybe in the distant future a version of Python will take a different stance, once we have more experience with how this works out in practice, but for Python 3.5 I want to restrict the scope of the upheaval.”
Van Rossum believed it’s possible (if the proposal is approved) to implement this feature in time for Python 3.5.
Van Rossum’s entire proposal is available here.