BT

Your opinion matters! Please fill in the InfoQ Survey!

Guido van Rossum Wants to Bring Type Annotations to Python

| by Sergio De Simone Follow 6 Followers on Aug 15, 2014. Estimated reading time: 2 minutes |

A note to our readers: As per your request we have developed a set of features that allow you to reduce the noise, while not losing sight of anything that is important. Get email and web notifications by choosing the topics you are interested in.

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 Stage
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.

Tell us what you think

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

Email me replies to any of my messages in this thread
Community comments

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

Email me replies to any of my messages in this thread

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

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT