[Python-ideas] Proposal: Use mypy syntax for function annotations

willvarfar at gmail.com willvarfar at gmail.com
Thu Aug 14 13:16:11 CEST 2014


I fully support formalizing Python 3's annotations for type checking.

I wrote - and use daily - my own type checker called obiwan
https://pypi.python.org/pypi/obiwan

Its a runtime type checker, and if enabled will check and enforce
types on every call.

I support the wider adoption and standardization of static type
checkers, but runtime checkers are still wanted for very dynamic code
and for checking external data e.g. I use obiwan for validating JSON.

One small detail is that I feel the obiwan annotations are more
pythonic than the mypy examples given.

E.g. instead of:

  from typing import List, Dict

  def word_count(input: List[str]) -> Dict[str, int]:
      ...

It would be:

  def word_count(input: [str]) -> {str, int}:
      ...

Obiwan does not check types within functions; I was unwilling to try
and overload comments!  You can invoke obiwan to check things
explicitly, but these are more as assertions.

Anyway, when I look at the mypy in-function annotations (where
comments are overloaded) I am cautious.  It would be far nicer if we
had annotations as part of the language instead, e.g. instead of:

  result = {}  #type: Dict[str, int]

It would be:

  result = {} -> {str, int}

where we use the -> arrow again.  I can see pros and cons for any
implementation (as we'd want the annotation to be both to declare a
type and to check a type, and want the annotation to be attached to
the variable forever etc) so this would need a full PEP treatment and
possibly be configurable as asserts are.

But proper annotation support in the language rather than overloading
comments would definitely be my preference.

/Will

/Will


More information about the Python-ideas mailing list