python / python/mypy

Positional-or-kw arg considered incompatible with positional-only argument

Open
#12,525 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

from typing_extensions import Protocol


class Func(Protocol):
    def __call__(self, __a: int, **kwargs: str) -> None:
        #              ^^^ positional-only arg
        ...
    # Same error when using PEP 570 positional-only syntax:
    # def __call__(self, a: int, /, **kwargs: str) -> None:


def myfunc(a: int, **kwargs: str) -> None:
    pass


f: Func = myfunc

Expected Behavior

In my view this should be allowed. Anyone calling f() promises to pass the 1st argument as positional. And myfunc accepts the 1st argument as positional or kwarg, so it should be considered compatible.

Actual Behavior

Mypy output:

positional_only.py:12: error: Incompatible types in assignment (expression has type "Callable[[int, KwArg(str)], Any]", variable has type "Func")
positional_only.py:12: note: "Func.__call__" has type "Callable[[int, KwArg(str)], Any]"

Your Environment

  • Mypy version used: 0.942
  • Mypy command-line flags: (none)
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.10.2
  • Operating system and version: macOS

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

Run the provided Python example with mypy and confirm the incompatible assignment diagnostic for the Protocol callable. Trace the callable compatibility handling for positional-only and positional-or-keyword parameters, then add a regression test showing that this assignment is accepted while incompatible calls remain rejected.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.