Title: Issue · GitHub
Open Graph Title: Issue · python/typeshed
X Title: Issue · python/typeshed
Description: Collection of library stubs for Python, with static types - Issue · python/typeshed
Open Graph Description: Collection of library stubs for Python, with static types - Issue · python/typeshed
X Description: Collection of library stubs for Python, with static types - Issue · python/typeshed
Opengraph URL: https://github.com/python/typeshed
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Should `asyncio.iscoroutinefunction` return some kind of `TypeGuard`?","articleBody":"I have the following use-case of a class that takes a regular function or a coroutine as an attribute, and then provides access to it through two properties. Basically the code looks like this:\r\n\r\n```python\r\n\"\"\"example.py\"\"\"\r\n\r\nfrom asyncio import iscoroutinefunction\r\nfrom typing import TypeVar, Generic, Callable, Awaitable, Any\r\n\r\nT = TypeVar('T')\r\n\r\nclass Response(Generic[T]):\r\n ...\r\n\r\nclass Request(Generic[T]):\r\n ...\r\n\r\nclass Foo:\r\n def __init__(self, callback: Callable[[Request[T]], Response[T] | Awaitable[Response[T]]]):\r\n self._callback = callback\r\n\r\n @property\r\n def func(self) -\u003e Callable[[Request[T]], Response[T]]:\r\n if iscoroutinefunction(self._callback):\r\n raise AttributeError()\r\n\r\n return self._callback\r\n\r\n @property\r\n def coro(self) -\u003e Callable[[Request[T]], Awaitable[Response[T]]:\r\n if not iscoroutinefunction(self._callback):\r\n raise AttributeError()\r\n\r\n return self._callback\r\n\r\nasync def bar(req: Request[T]) -\u003e Response[T]:\r\n ...\r\n\r\nfoo = Foo(bar)\r\n\r\n```\r\n\r\nNow, I expected mypy to give no errors due to type narrowing from `iscoroutinefunction, but instead I get\r\n```\r\nexample.py:21: error: Incompatible return value type (got \"Callable[[Request[T]], Union[Response[T], Awaitable[Response[T]]]]\", expected \"Callable[[Request[T]], Response[T]]\")\r\nexample.py:28: error: Incompatible return value type (got \"Callable[[Request[T]], Union[Response[T], Awaitable[Response[T]]]]\", expected \"Callable[[Request[T]], Awaitable[Response[T]]]\")\r\n```\r\n\r\nAfter finding this, I started searching through the issues in both mypy and typeshed but couldn't find anything related to `iscoroutinefunction`. I then looked into typeshed and saw that the return type of `iscoroutinefunction` is just `bool`, and not a `TypeGuard`, despite the similar `iscoroutine` having one. This makes me think that `iscoroutinefunction` is annotated like that on purpose.\r\n\r\nSo my question is, should the return type of `asyncio.iscoroutinefunction` be a `TypeGuard` of some kind?\r\n\r\nI'm new to the more advanced features of Python typing, and my naive implementation would be something like\r\n```python\r\nfrom typing import TypeVar, TypeGuard\r\n\r\nT = TypeVar('T')\r\n\r\ndef iscoroutinefunction(func: T) -\u003e TypeGuard[T]: ...\r\n```\r\n\r\nIf you feel like this should be changed, I'd be happy to submit a PR once I know what the type should be.\r\n","author":{"url":"https://github.com/ajoino","@type":"Person","name":"ajoino"},"datePublished":"2022-06-01T18:46:20.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/8009/typeshed/issues/8009"}
| route-pattern | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:69fca355-24a8-3f0a-9a9b-b4fbe3e5360c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 900A:E2F0F:77B5F3:AC6904:6A621180 |
| html-safe-nonce | 8f7b1ba8bf18a67ead04fc0b607347f8723212cfdefff37ce4c453932da09f40 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MDBBOkUyRjBGOjc3QjVGMzpBQzY5MDQ6NkE2MjExODAiLCJ2aXNpdG9yX2lkIjoiNjcyNzI3MzIwNTI1NzYwNTUwNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 0aad1fcfaf1b080f1d95bc5a3bac81cdf925847be071f01183c9dc3bcbb9b210 |
| hovercard-subject-tag | repository:31696383 |
| 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/python/typeshed/issues/8009 |
| twitter:image | https://opengraph.githubassets.com/efc3d7b1334678da98678eb63f0b67b6b1d02e217320f984c9457d37a68215cd/python/typeshed |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/efc3d7b1334678da98678eb63f0b67b6b1d02e217320f984c9457d37a68215cd/python/typeshed |
| og:image:alt | Collection of library stubs for Python, with static types - Issue · python/typeshed |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | ccf61cb7ab435725b916ed640f57c45b2c4721db9469bc32656080efc5214b3e |
| turbo-cache-control | no-cache |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| 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 | a7585b3d2ee639495a70ce464d0a18bad88ea12c |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width