Skip to content

Passing generic callable to function fails #6376

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

Closed
saulshanabrook opened this issue Feb 11, 2019 · 1 comment
Closed

Passing generic callable to function fails #6376

saulshanabrook opened this issue Feb 11, 2019 · 1 comment

Comments

@saulshanabrook
Copy link

I am trying to type a function that takes in a function, and a value, and returns the arguments of the function. For example, this is how it would look supporting two argument functions:

T = typing.TypeVar("T")
U = typing.TypeVar("U")
V = typing.TypeVar("V")
X = typing.TypeVar("X")


def extract_args(fn: typing.Callable[[T, U], V], ret: V) -> typing.Tuple[T, U]:
    ...

This works great when the callable I pass in is not generic, like this:

def hi(a: int, b: str) -> str:
    ...

reveal_type(extract_args(hi, "return"))
# Revealed type is 'Tuple[builtins.int*, builtins.str*]'

However, if I try to make the function generic, it doesn't seem to work:

def hi_generic(a: int, b: X) -> X:
    ...


reveal_type(extract_args(hi_generic, "return"))
# Argument 1 to "extract_args" has incompatible type "Callable[[int, X], X]"; expected "Callable[[int, X], str]"
# Revealed type is 'Tuple[builtins.int*, X`-1]'

I would expect it to infer fn has type Callable[[int, str], str] since I am passing in the return value of type str so the second arg also has to be a str.

This might be the same issue as #3193.

If anyone has a work around for how I could get a function like this to type, they would be much appreciated!

@ilevkivskyi
Copy link
Member

Thanks for reporting! Actually both are duplicates of #1317 (already high priority).

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

No branches or pull requests

2 participants