Title: Allow `types.GeneratorType` to be used instead of `collections.abc.Generator` as the return type of generator functions · Issue #20522 · python/mypy · GitHub
Open Graph Title: Allow `types.GeneratorType` to be used instead of `collections.abc.Generator` as the return type of generator functions · Issue #20522 · python/mypy
X Title: Allow `types.GeneratorType` to be used instead of `collections.abc.Generator` as the return type of generator functions · Issue #20522 · python/mypy
Description: Feature Currently, mypy only allows collections.abc.Generator and its supertypes as the return type of generator functions, and as a result, it cannot infer the correct return type for awaitable objects that define the __await__() method...
Open Graph Description: Feature Currently, mypy only allows collections.abc.Generator and its supertypes as the return type of generator functions, and as a result, it cannot infer the correct return type for awaitable ob...
X Description: Feature Currently, mypy only allows collections.abc.Generator and its supertypes as the return type of generator functions, and as a result, it cannot infer the correct return type for awaitable ob...
Opengraph URL: https://github.com/python/mypy/issues/20522
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow `types.GeneratorType` to be used instead of `collections.abc.Generator` as the return type of generator functions","articleBody":"**Feature**\n\nCurrently, mypy only allows [`collections.abc.Generator`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Generator) and its supertypes as the return type of generator functions, and as a result, it cannot infer the correct return type for awaitable objects that define the [`__await__()`](https://docs.python.org/3/reference/datamodel.html#object.__await__) method via [`types.GeneratorType`](https://docs.python.org/3/library/types.html#types.GeneratorType):\n\n```python\nimport asyncio\n\nfrom collections.abc import Generator\nfrom types import GeneratorType\nfrom typing import Never, reveal_type\n\n\nclass A:\n def __await__(self) -\u003e GeneratorType[Never, Never, bool]:\n return True\n yield # generator definition\n\n\nclass B:\n def __await__(self) -\u003e Generator[Never, Never, bool]:\n return True\n yield # generator definition\n\n\nasync def main() -\u003e None:\n reveal_type(await A()) # revealed type is \"Any\"\n reveal_type(await B()) # revealed type is \"builtins.bool\"\n\n reveal_type(A().__await__().gi_running) # revealed type is \"builtins.bool\"\n reveal_type(B().__await__().gi_running) # has no attribute \"gi_running\"\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n\n```\nexample.py:9: error: The return type of a generator function should be \"Generator\" or one of its supertypes [misc]\nexample.py:21: note: Revealed type is \"Any\"\nexample.py:22: note: Revealed type is \"builtins.bool\"\nexample.py:24: note: Revealed type is \"builtins.bool\"\nexample.py:25: error: \"Generator[Never, Never, bool]\" has no attribute \"gi_running\" [attr-defined]\nexample.py:25: note: Revealed type is \"Any\"\nFound 2 errors in 1 file (checked 1 source file)\n```\n\nI propose allowing the use of [`types.GeneratorType`](https://docs.python.org/3/library/types.html#types.GeneratorType) with subsequent inference of the correct return type.\n\n**Pitch**\n\nFirst, [pyright](https://github.com/microsoft/pyright) already has the expected behavior:\n\n```\n/home/user/workspace/example.py\n /home/user/workspace/example.py:21:17 - information: Type of \"await A()\" is \"bool\"\n /home/user/workspace/example.py:22:17 - information: Type of \"await B()\" is \"bool\"\n /home/user/workspace/example.py:24:17 - information: Type of \"A().__await__().gi_running\" is \"bool\"\n /home/user/workspace/example.py:25:17 - information: Type of \"B().__await__().gi_running\" is \"Unknown\"\n /home/user/workspace/example.py:25:33 - error: Cannot access attribute \"gi_running\" for class \"Generator[Never, Never, bool]\"\n Attribute \"gi_running\" is unknown (reportAttributeAccessIssue)\n1 error, 0 warnings, 4 informations\n```\n\nSecond, this relates to the eternally controversial python/typing#1480. In python/typeshed#10816, false properties related to native objects were removed in favor of the [`types`](https://docs.python.org/3/library/types.html) module. You could say that we can use [`isinstance`](https://docs.python.org/3/library/functions.html#isinstance)/[`inspect.isgenerator`](https://docs.python.org/3/library/inspect.html#inspect.isgenerator) to narrow [`collections.abc.Generator`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Generator) down to [`types.GeneratorType`](https://docs.python.org/3/library/types.html#types.GeneratorType), but... it is not that simple:\n\n```python\n x = B().__await__()\n reveal_type(x) # typing.Generator[Never, Never, builtins.bool]\n\n if isgenerator(x):\n reveal_type(x) # types.GeneratorType[Any, Any, Any]\n\n if isinstance(x, GeneratorType):\n reveal_type(x) # types.GeneratorType[Any, Any, Any]\n```\n\nI think that allowing the use of [`types.GeneratorType`](https://docs.python.org/3/library/types.html#types.GeneratorType) would make it easier to inspect native generator objects. Especially since [`collections.abc.Generator`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Generator) can also be used to define custom generator types.\n\nRelated: #18635.","author":{"url":"https://github.com/x42005e1f","@type":"Person","name":"x42005e1f"},"datePublished":"2026-01-04T18:01:57.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/20522/mypy/issues/20522"}
| 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:f7198f7b-9bc5-05a9-29b9-681b0bcec095 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8BD4:1E51E8:D37FDE:12F7DB3:6A634C68 |
| html-safe-nonce | 747ca176df8eb5a90dc3e8f38e0570aff5593913fc86a1ee9f59537961fbb035 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QkQ0OjFFNTFFODpEMzdGREU6MTJGN0RCMzo2QTYzNEM2OCIsInZpc2l0b3JfaWQiOiI5MjEyMTUyMzg1NzY2NjQwNzQ0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | ad6115c4ca0458ea27537fc4ea4b2cb25bd129f4b25285c78776f88fa6417c0b |
| hovercard-subject-tag | issue:3779491303 |
| 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/20522/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f1923b8dc25982ecf8b28b67902ec11aa1d33b74e4b1200f8c526545a21f60a1/python/mypy/issues/20522 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f1923b8dc25982ecf8b28b67902ec11aa1d33b74e4b1200f8c526545a21f60a1/python/mypy/issues/20522 |
| og:image:alt | Feature Currently, mypy only allows collections.abc.Generator and its supertypes as the return type of generator functions, and as a result, it cannot infer the correct return type for awaitable ob... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | x42005e1f |
| hostname | github.com |
| expected-hostname | github.com |
| None | 59e55daad7174ca59d63c6974d58276ccb5477442e550bebb3c035e1bef11c94 |
| 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 | 990295d92a4cc7b63fbbd83a046217cd7d77d49c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width