python / python/mypy

Reject arguments that are both keyword and positional only

Open
#8,269 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug priority-2-low topic-calls
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running mypy on the two Callable/Protocol reproductions in the issue and compare their assignment diagnostics. Trace the handling of __args and __arg in callable compatibility, then verify that arguments treated as both keyword and positional are rejected with a clear diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.