BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Guido van Rossum Wants to Bring Type Annotations to Python

Guido van Rossum Wants to Bring Type Annotations to Python

This item in japanese

Bookmarks

Guido van Rossum, best known as designer of the Python programming language, recently sent out a proposal on the python-ideas mailing list for adding type annotations to Python function declarations. The proposal aims at bringing to Python the benefits provided by static typing without changing Python's dynamic typing nature and interpreter behaviour.

The goal is to make it possible to add type checking annotations to 3rd party modules (and even to the stdlib) while allowing unaltered execution of the program by the (unmodified) Python 3.5 interpreter

Guido's proposal for adding type annotations rests on ideas from Bob Ippolito, who has argued in favour of bringing some of Haskell's features to Python and other languages; and Jukka Lehtosalo, author of mypy, an experimental Python variant that aims to combine the benefits of dynamic typing and static typing.

Mypy is a central piece in Guido's proposal. Indeed, Guido says, mypy could be seen as a lint-like static checker for Python to be used to check a program's type correctness at compile time, or when running it under mypy Python interpreter. On the other hand, the program execution would remain unaffected by the type annotations when run under Python official interpreter, so no runtime type checking overhead would ensue. The key requirement for this to be possible is that the syntax used for type annotations must be valid Python 3 syntax, which is one of mypy's features. Indeed, mypy uses function annotations, a Python 3 syntax for adding arbitrary metadata annotations to functions, to specify type signatures.

As an example of mypy basic syntax, the following Python definition:

def fib(n):
    a, b = 0, 1
    while a < n:
        print(a)
        a, b = b, a+b

can be modified in mypy to specify a type signature:

def fib(n: int) -> None:
    a, b = 0, 1
    while a < n:
        print(a)
        a, b = b, a+b

In cases where no suitable Python syntax is available to specify a type, mypy uses comments, like in the following example provided by Guido van Rossum in his proposal:

  def word_count(input: List[str]) -> Dict[str, int]:
      result = {}  #type: Dict[str, int]
      for line in input:
          for word in line.split():
              result[word] = result.get(word, 0) + 1
      return result

Given all of this, Guido suggests that a PEP could be written, accepted and implemented in time for Python 3.5. This would entail two main branches of work:

  • Reassessing the definition of function annotations to restrict their use to type annotations.

  • Providing a specification for what to add to Python 3.5, which Guido suggests be reduced to a minimum.

The actual type checker will not be integrated with the Python interpreter, and it will not be checked into the CPython repository. The only thing that needs to be added to the stdlib is a copy of mypy's typing.py module. This module defines several dozen new classes (and a few decorators and other helpers) that can be used in expressing argument types. If you want to type-check your code you have to download and install mypy and run it separately.

Guido's proposal raised some interest both on Python-ideas mailing list and on reddit. Besides positive reactions from several contributors to the discussion, concerns were raised about restricting function annotations to type signature specifications, and about the use of comments to convey type definitions.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT