python / python/mypy

Bad interaction between ParamSpec and TypeVarTuple

Open
#19,855 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

This should type check, but

from typing import Callable, TypeVarTuple

def run[T, **P](fn: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: ...
def coroutine_or_error[*Us](async_fn: Callable[[*Us], int], *args: *Us) -> int: ...

def test[*Ts](async_fn: Callable[[*Ts], int], args: tuple[*Ts]) -> None:
    run(coroutine_or_error, async_fn, *args)

https://mypy-play.net/?mypy=latest&python=3.12&gist=e43aee5d75dd536ed2529b3dc052e630 gives

Argument 1 to "run" has incompatible type "Callable[[Callable[[VarArg(*Us)], int], VarArg(*Us)], int]"; expected "Callable[[Callable[[VarArg(*Ts)], int], VarArg(object)], int]"  [arg-type]

Example derived from https://github.com/python-trio/trio/blob/f1039299644dfaf6dcd60ed29a4a3a51494d09a4/src/trio/_core/_run.py#L1970

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

Reproduce the snippet in the linked mypy-play example and compare the reported ParamSpec and TypeVarTuple diagnostic. Read the referenced Trio entry point at src/trio/_core/_run.py#L1970 to understand the derived use case. Done means the example type checks without the incompatible Callable error.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.