Skip to content

Resolve ParamSpec with the matching overload #1838

@leddy231

Description

@leddy231

Summary

Upgraded from 0.0.1a26 to 0.0.1a33 and noticed issues with asyncio.to_thread in combination with overloaded functions. Initially noticed with open but can be reduced to some simple example:

import asyncio
from typing import overload


def normal(a: str) -> None:
    pass


@overload
def overloaded(a: str) -> None:
    pass


@overload
def overloaded(a: int) -> None:
    pass


def overloaded(a: str | int) -> None:
    pass


t1 = normal("test")
t2 = overloaded("test")
t3 = asyncio.to_thread(normal, "test")
t4 = asyncio.to_thread(overloaded, "test")

ty check gives:

error[invalid-argument-type]: Argument to function `to_thread` is incorrect
  --> test.py:26:36
   |
24 | t2 = overloaded("test")
25 | t3 = asyncio.to_thread(normal, "test")
26 | t4 = asyncio.to_thread(overloaded, "test")
   |                                    ^^^^^^ Expected `Overload[(a: str) -> Unknown, (a: int) -> Unknown]`, found `Literal["test"]`
   |
info: Function defined here
  --> stdlib/asyncio/threads.pyi:12:11
   |
10 | _R = TypeVar("_R")
11 |
12 | async def to_thread(func: Callable[_P, _R], /, *args: _P.args, **kwargs: _P.kwargs) -> _R:
   |           ^^^^^^^^^                            -------------- Parameter declared here
13 |     """Asynchronously run function *func* in a separate thread.
   |
info: rule `invalid-argument-type` is enabled by default

Version

ty 0.0.1a33

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcallsIssues relating to call-signature checking and diagnosticsoverloads

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions