New parametrized type annotations for run_in_threadpool incompatible with generic functions #1754
-
After #1383 was released, Reproduction code: from typing import TypeVar
from starlette.concurrency import run_in_threadpool
T = TypeVar("T")
async def func(value: T) -> T:
return value
async def main() -> None:
val = await run_in_threadpool(func, 1)
reveal_type(val) $ mypy example.py
example.py:13: error: Argument 2 to "run_in_threadpool" has incompatible type "int"; expected "T" [arg-type]
example.py:14: error: Value of type "Coroutine[Any, Any, T]" must be used [unused-coroutine]
example.py:14: note: Are you missing an await?
example.py:14: note: Revealed type is "typing.Coroutine[Any, Any, T`-1]"
Found 2 errors in 1 file (checked 1 source file) The concrete situation where I encountered this was wrapping a I might recommend reverting the part of #1383 related to |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Just for reference, this is your original issue: import glob
from typing import TypeVar, ParamSpec, Callable
T = typing.TypeVar("T")
P = ParamSpec("P")
async def run_in_threadpool(
func: typing.Callable[P, T], *args: P.args, **kwargs: P.kwargs
) -> T:
if kwargs: # pragma: no cover
# run_sync doesn't accept 'kwargs', so bind them in here
func = functools.partial(func, **kwargs)
return await anyio.to_thread.run_sync(func, *args)
async def main() -> None:
await run_in_threadpool(glob.glob, "*") This leads to:
I've asked about this on the We can already turn this into an issue if you want. 😗 |
Beta Was this translation helpful? Give feedback.
-
This is already an issue on mypy: python/mypy#12278 (comment) As they are positive on implementing this, we could either not revert and wait for them to fix it, or revert the type annotation on |
Beta Was this translation helpful? Give feedback.
-
This was solved. 👍 |
Beta Was this translation helpful? Give feedback.
This was solved. 👍