-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User-defined TypeGuard for Callable is coroutine function has errors #13710
Comments
Just as a note, I've used this code to try to test this: from typing import ParamSpec, Callable, TypeGuard, Any, Coroutine, Concatenate
import inspect
import asyncio
_PArgs = ParamSpec("_PArgs")
def iscoroutinefunc(func: Callable[_PArgs, Any]) -> TypeGuard[Callable[_PArgs, Coroutine[Any, Any, Any]]]:
return inspect.iscoroutinefunction(func)
_PRouteParams = ParamSpec("_PRouteParams")
async def main() -> None:
async def my_coro(x: int, y: int, z: int) -> str:
await asyncio.sleep(1)
return str(x + y + z)
async def execute_if_coro(
my_func_or_coro: Callable[Concatenate[int, _PRouteParams], Any], *args: _PRouteParams.args, **kwargs: _PRouteParams.kwargs
) -> None:
if iscoroutinefunc(my_func_or_coro):
await my_func_or_coro(3, *args, **kwargs)
else:
my_func_or_coro(3, *args, **kwargs)
await execute_if_coro(my_coro, 5, z=6) (Just posting so I do not need to change the code next time I check. Needed to add the imports and the |
@A5rocks Thank you very much for your PR I see all checks are approved will it be merged and released soon? |
Hi :) I haven't put in time recently to the PR but I have 2 main things I need to do before it can really be looked at for real:
I hope to have that done in a few days! |
FYI this is fixed as of Mypy 1.2! Turns out some other changed fixed this, oops. |
|
Should we add a new test case for this? |
Bug Report
Defining a TypeGuard for callable to check if coroutine doesn't work when passed a generic callable with Concatenate.
To Reproduce
Expected Behavior
I expected mypy to infer in
execute_if_coro
thatmy_func_or_coro
can receive an a positional integer before the additional *args and **kwargs and that the result will be awaitable usingawait
keyword.Actual Behavior
Mypy reports 2 errors
Your Environment
mypy.ini
(and other config files):follow_imports = "normal"
ignore_errors = false
implicit_reexport = false
warn_redundant_casts = true
warn_unused_ignores = true
disallow_any_generics = true
disallow_untyped_defs = true
check_untyped_defs = true
allow_redefinition = false
local_partial_types = true
strict_optional = true
strict_equality = true
warn_unused_configs = true
warn_unreachable = true
warn_no_return = true
no_implicit_optional = true
strict = true
The text was updated successfully, but these errors were encountered: