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

Type inference from callable argument types depends on order of kwonly args #6492

Closed
oremanj opened this issue Feb 28, 2019 · 3 comments
Closed
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-type-variables

Comments

@oremanj
Copy link
Contributor

oremanj commented Feb 28, 2019

Running mypy 0.670 on the following example:

$ cat t.py
from typing import Callable, TypeVar, List
from mypy_extensions import NamedArg

T = TypeVar("T")

def invoke(fn: Callable[[NamedArg(List[T], "named")], None]) -> T:
    arg: List[T] = []
    fn(named=arg)
    return arg[0]

def callback(*, other: bool = False, named: List[int]) -> None:
    named.append(42 + other)

reveal_type(invoke(callback))

produces:

t.py:14: error: Revealed type is '<nothing>'
t.py:14: error: Argument 1 to "invoke" has incompatible type "Callable[[DefaultNamedArg(bool, 'other'), NamedArg(List[int], 'named')], None]"; expected "Callable[[NamedArg(List[<nothing>], 'named')], None]"

I was expecting Revealed type is 'builtins.int*'. If I swap the order of the keyword-only arguments to callback, so that the one I'm matching on is before the one that's getting defaulted, I get the expected behavior. If I modify the example so that there aren't any generic types in the signature of invoke that need inference, i.e. replacing T with int, I get the expected behavior no matter what order the callback arguments are declared in.

@ilevkivskyi
Copy link
Member

I also tried this with a callback protocol, and result is the same. So I assume this is a bug in constraint inference for callable types.

@davidhalter
Copy link

This is passing now, so I feel like this should be closed.

See also https://mypy-play.net/?mypy=latest&python=3.12&gist=21fd643af21f5f184c24815e95a5c11e

@hauntsaninja
Copy link
Collaborator

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-type-variables
Projects
None yet
Development

No branches or pull requests

4 participants