Title: User-defined TypeGuard for Callable is coroutine function has errors · Issue #13710 · python/mypy · GitHub
Open Graph Title: User-defined TypeGuard for Callable is coroutine function has errors · Issue #13710 · python/mypy
X Title: User-defined TypeGuard for Callable is coroutine function has errors · Issue #13710 · python/mypy
Description: Bug Report Defining a TypeGuard for callable to check if coroutine doesn't work when passed a generic callable with Concatenate. To Reproduce Write this code _PArgs = ParamSpec("_PArgs") def iscoroutinefunc(func: Callable[_PArgs, Any]) -...
Open Graph Description: Bug Report Defining a TypeGuard for callable to check if coroutine doesn't work when passed a generic callable with Concatenate. To Reproduce Write this code _PArgs = ParamSpec("_PArgs") def iscoro...
X Description: Bug Report Defining a TypeGuard for callable to check if coroutine doesn't work when passed a generic callable with Concatenate. To Reproduce Write this code _PArgs = ParamSpec("_PArgs&quo...
Opengraph URL: https://github.com/python/mypy/issues/13710
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"User-defined TypeGuard for Callable is coroutine function has errors","articleBody":"**Bug Report**\r\n\r\nDefining a TypeGuard for callable to check if coroutine doesn't work when passed a generic callable with Concatenate.\r\n\r\n**To Reproduce**\r\n\r\n1. Write this code\r\n\r\n```\r\n_PArgs = ParamSpec(\"_PArgs\")\r\n\r\ndef iscoroutinefunc(func: Callable[_PArgs, Any]) -> TypeGuard[Callable[_PArgs, Coroutine[Any, Any, Any]]]:\r\n return inspect.iscoroutinefunction(func)\r\n```\r\n\r\n2. Call the type guard like this:\r\n\r\n```\r\n_PRouteParams = ParamSpec(\"_PRouteParams\")\r\nasync def my_coro(x: int, y: int, z: int) -> str:\r\n await asyncio.sleep(1)\r\n return str(x + y + z)\r\n\r\n async def execute_if_coro(\r\n my_func_or_coro: Callable[Concatenate[int, _PRouteParams], Any], *args: _PRouteParams.args, **kwargs: _PRouteParams.kwargs\r\n ) -> None:\r\n if iscoroutinefunc(my_func_or_coro):\r\n await my_func_or_coro(3, *args, **kwargs)\r\n else:\r\n my_func_or_coro(3, *args, **kwargs)\r\n\r\n await execute_if_coro(my_coro, 5, z=6)\r\n```\r\n\r\n3. Run mypy on above code with \"strict=True\" setting\r\n\r\n**Expected Behavior**\r\n\r\nI expected mypy to infer in `execute_if_coro` that `my_func_or_coro` can receive an a positional integer before the additional *args and **kwargs and that the result will be awaitable using `await` keyword.\r\n\r\n**Actual Behavior**\r\n\r\nMypy reports 2 errors\r\n\r\n- Argument 1 to \"iscoroutinefunc\" has incompatible type \"Callable[[int, **_PRouteParams], Any]\"; expected \"Callable[[int, **_PRouteParams], Any]\r\n- Argument 1 has incompatible type \"int\"; expected \"[int, **_PRouteParams.args]\"\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 0.971\r\n- Mypy command-line flags: N/A\r\n- Mypy configuration options from `mypy.ini` (and other config files):\r\n\r\nfollow_imports = \"normal\"\r\nignore_errors = false\r\nimplicit_reexport = false\r\nwarn_redundant_casts = true\r\nwarn_unused_ignores = true\r\ndisallow_any_generics = true\r\ndisallow_untyped_defs = true\r\ncheck_untyped_defs = true\r\nallow_redefinition = false\r\nlocal_partial_types = true\r\nstrict_optional = true\r\nstrict_equality = true\r\nwarn_unused_configs = true\r\nwarn_unreachable = true\r\nwarn_no_return = true\r\nno_implicit_optional = true\r\nstrict = true\r\n\r\n- Python version used: 3.10.4\r\n- Operating system and version: Windows 11\r\n","author":{"url":"https://github.com/EldarSehayekZenity","@type":"Person","name":"EldarSehayekZenity"},"datePublished":"2022-09-23T00:19:44Z","url":"https://github.com/python/mypy/issues/13710"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d7bf5762-9648-22ef-176f-624b11e0bd7e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BC3C:1CF166:18B0DB2:21414DD:6AFFF4DC |
| html-safe-nonce | 06d14a9ad261e09be40f91128dd0c4fdead7fc4f0c5150e1532eb7d0e6c574df |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQzNDOjFDRjE2NjoxOEIwREIyOjIxNDE0REQ6NkFGRkY0REMiLCJ2aXNpdG9yX2lkIjoiMzcyNTk0NjQ3MjUyNTQ2MDcwMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3c13d6f67b5326ae7a04020c0af1d875d4ca27eae534fd8e4681e679193e07cc |
| hovercard-subject-tag | issue:1383147144 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/13710/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9a44fe44485f139d40aa743533fe261f6362646cd019dc54e1b8240bcf3fc597/python/mypy/issues/13710 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9a44fe44485f139d40aa743533fe261f6362646cd019dc54e1b8240bcf3fc597/python/mypy/issues/13710 |
| og:image:alt | Bug Report Defining a TypeGuard for callable to check if coroutine doesn't work when passed a generic callable with Concatenate. To Reproduce Write this code _PArgs = ParamSpec("_PArgs") def iscoro... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | EldarSehayekZenity |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width