[Python-Dev] Type hints -- a mediocre programmer's reaction (original) (raw)

Nikolaus Rath Nikolaus at rath.org
Mon Apr 20 21:51:15 CEST 2015


On Apr 20 2015, Harry Percival <hjwp2 at cantab.net> wrote:

My first reaction to type hints was "yuck", and I'm sure I'm not the only one to think that. viz (from some pycon slides):

def zipmap(f: Callable[[int, int], int], xx: List[int], yy: List[int]) -> List[Tuple[int, int, int]]: arg. and imagine it with default arguments.

This is indeed ugly as hell and I certainly would not want to see this in any Python file I'm working with.

However, I always assumed that whatever tools consume these annotations are expected to be good enough at automatic type inference that something like this would never be necessary?

In practice, I would hope that the above simplifies to

def zipmap(f, xx: List[int], yy: List[int]):
   

because (just picking a probably buggy random implementation)

   zz = [] # --> z must be List[A]
   for i in range(min(len(xx), len(yy))):
      x = xx[i]   # --> x must be int
      y = xx[i]   # --> y must be int
      z = f(x,y)  # --> f must be Callable[(int,int], B]
      zz[i] = (x,y,z)   # --> A must be Tuple[int,int,B]

    return zz # --> return value must be List[Tuple[int,int,B]]

it doesn't catch that B = int, but I think that's acceptable.

Is this not the case? Are we really expecting people to write stuff like the above?

Best, -Nikolaus

-- GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F

         »Time flies like an arrow, fruit flies like a Banana.«


More information about the Python-Dev mailing list