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

Reject arguments that are both keyword and positional only #8269

Open
c4f3a0ce opened this issue Jan 9, 2020 · 3 comments
Open

Reject arguments that are both keyword and positional only #8269

c4f3a0ce opened this issue Jan 9, 2020 · 3 comments
Labels
bug mypy got something wrong priority-2-low topic-calls Function calls, *args, **kwargs, defaults

Comments

@c4f3a0ce
Copy link

c4f3a0ce commented Jan 9, 2020

I tried to use Calalble protocol like this:

from typing import Callable, Protocol

class F(Protocol):
    def __call__(self, *__args: str,  __arg: int) -> int: ...


def f(*__args: str, __arg: int) -> int: ...

f_: F = f

but it fails with quite confusing message:

error: Incompatible types in assignment (expression has type "Callable[[VarArg(str), NamedArg(int)], int]", variable has type "F")
note: "F.__call__" has type "Callable[[VarArg(str), NamedArg(int)], int]"
Found 1 error in 1 file (checked 1 source file)

If I move varargs, things pass:

class G(Protocol):
    def __call__(self, __arg: int, *__args: str) -> int: ...
    
def g(__arg: int, *__args: str) -> int: ...
g_: G = g

I cannot say if this is a bug, or expected behaviour with confusing message.

@JelleZijlstra
Copy link
Member

Not sure it's related, but by convention mypy treats arguments prefixed with two underscores as positional-only, so that may lead to confusion here.

@msullivan
Copy link
Collaborator

Yeah I think this is expected behavior with a confusing message? Double underscore means positional-only and you are putting in a keyword-only position, which then doesn't match. Probably we should error on arguments that are both keyword-only and positional-only.

@msullivan msullivan changed the title Varargs with Callable Protocols Reject arguments that are both keyword and positional only Jan 9, 2020
@c4f3a0ce
Copy link
Author

c4f3a0ce commented Jan 9, 2020

Makes perfect sense. Thanks @JelleZijlstra and @msullivan.

@AlexWaygood AlexWaygood added bug mypy got something wrong topic-calls Function calls, *args, **kwargs, defaults labels Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-2-low topic-calls Function calls, *args, **kwargs, defaults
Projects
None yet
Development

No branches or pull requests

4 participants