Python 2.7 type inference and TypeVar

Answered

I'm attempting to use TypeVar in type annotations in Python 2.7 and it doesn't seem to be working as expected.

In this code:

from typing import TypeVar

T = TypeVar('T')


def f2(a): # type: (T)->T
return a


def f1(): # type: ()->None
v1 = f2('bob') + f2(1)

I would expect to get a warning on the v1 line that I'm trying to concatenate a string and an integer. If I show the documentation for f2 I get an inferred type of (Any)->Any rather than (TypeVar('T')) -> TypeVar('T')

If I run this under Python 3.5 with the 3.5 type syntax I get the expected result.

0
1 comment

Hi Mark, sorry for the late reply.

It's an unfortunate issue related to the way we resolve symbols in type comments. We will fix it soon, please follow PY-19807.

0

Please sign in to leave a comment.