ParamSpec seems to get lost with partial()
Open
Nobody has claimed this yet.
bug
topic-paramspec
topic-runtime-semantics
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Trying to get ParamSpec working for async-lru, and currently testing out the new partial() support added last year, but it doesn't seem to work with our wrapper class.
To Reproduce
from functools import partial
from typing import Any, Callable, Coroutine, Generic, ParamSpec, TypeVar
_R = TypeVar("_R")
_P = ParamSpec("_P")
class Wrapper(Generic[_P, _R]):
def __init__(self, fn: Callable[_P, Coroutine[Any, Any, _R]]) -> None:
self.fn = fn
async def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R:
return await self.fn(*args, **kwargs)
async def coro(val: int) -> int:
return val
async def test() -> None:
coro_wrapped = Wrapper(coro)
reveal_type(coro_wrapped) # Revealed type is "Wrapper[[val: builtins.int], builtins.int]"
await coro_wrapped(3, 2) # error: Too many arguments for "__call__" of "Wrapper" [call-arg]
partial_wrapped = Wrapper(partial(coro, 2))
reveal_type(partial_wrapped) # Revealed type is "Wrapper[[*args: Any, **kwargs: Any], builtins.int]"
await partial_wrapped(4) # XXX: Should be an error here.
Your Environment
- Mypy version used: 1.14.1
- Python version used: 3.9
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied Python 3.9 reproducer with mypy 1.14.1, then trace how functools.partial and ParamSpec inference produce the revealed Wrapper type. Done means the partial-wrapped coroutine retains its bound signature and the extra argument is rejected as shown in the first case.
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
- Clearly specified
- Newbie friendliness
- 48/100