Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying functions to generic functions doesn't seem to work properly #288

Closed
ecprice opened this issue Jul 14, 2014 · 2 comments
Closed
Labels
bug mypy got something wrong

Comments

@ecprice
Copy link
Contributor

ecprice commented Jul 14, 2014

from typing import Function, typevar

A = typevar('A')
B = typevar('B')

def square(n: int) -> int:
    return n*n

def id(f: Function[[A], B]) -> Function[[A], B]:
    return f

g = id(square)

print(g(1))

the internal representation of the type of g seems to be "def ('None') -> builtins.int*" which isn't valid.

@jswalden
Copy link
Contributor

ExpressionChecker.infer_function_type_arguments decides for id(square), via ExpressionChecker.get_arg_infer_passes, that the function argument type should be resolved in the second pass. Or something along those lines. It then proceeds to use None as argument type, which "type" propagates via return value until the embedded argument type gets compared against int for the 1.

It's not immediately clear to me, looking at this code, why it shouldn't iteratively infer types until a fixed point is reached.

@jswalden
Copy link
Contributor

Reduced further:

from typing import Function, typevar

A = typevar('A')

def voidify(n: int) -> None:
    pass

def identity(f: Function[[A], None]) -> Function[[A], None]:
    return f

identity(voidify)(3)

The return type doesn't matter, can be a typevar or a builtin type or None. What specifically matters is that an argument be a typevar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants