python / python/mypy

Callables like `operator.call` produce `arg-type` error with callables that take `*args: Unpack[Ts]` and >= 1 keyword argument

Open
#16,662 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-pep-646
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

operator.call and similar ParamSpec callables (e.g. concurrent.futures.Executor.submit) produce an arg-type error when doing call(fn, fn2, some_kwarg=...) where fn has signature (fn2: Callable[[Unpack[Ts]], T], *args: Unpack[Ts], some_kwarg: ...) -> ...

this looks like a TypeVarTuple analog of #16405.

To Reproduce

import operator
from collections.abc import Callable
from typing import TypeVar
from typing import TypeVarTuple
from typing import Unpack

T = TypeVar("T")
Ts = TypeVarTuple("Ts")


def run(func: Callable[[Unpack[Ts]], T], *args: Unpack[Ts], some_kwarg: str = "asdf") -> T:
    raise NotImplementedError


def foo() -> str:
    raise NotImplementedError


run(foo)  # OK
operator.call(run, foo)  # OK
run(foo, some_kwarg="a")  # OK
operator.call(run, foo, some_kwarg="a")
# error: Argument 1 to "call" has incompatible type "Callable[[Callable[[VarArg(*Ts)],
# T], VarArg(*Ts), DefaultNamedArg(str, 'some_kwarg')], T]"; expected
# "Callable[[Callable[[], str], str], str]"  [arg-type]

Expected Behavior

no error.

Actual Behavior

see comments in the reproducer above.

Your Environment

  • Mypy version used: 1.7.1 (compiled) & latest master (uncompiled)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: CPython 3.12.1

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 the provided reproducer with mypy on current master and compare the behavior with issue #16405. Trace the handling of operator.call, ParamSpec, and TypeVarTuple callables; done means the keyword-argument call produces no arg-type error while the existing checks remain correct.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.