Title: No Generator is considered to be an instance of GeneratorType · Issue #5496 · python/typeshed · GitHub
Open Graph Title: No Generator is considered to be an instance of GeneratorType · Issue #5496 · python/typeshed
X Title: No Generator is considered to be an instance of GeneratorType · Issue #5496 · python/typeshed
Description: Bug Report types.GeneratorType is documented as "The type of generator-iterator objects, created by generator functions." Therefore I would expect that a value annotated using typing.Generator would be considered an instance of Generator...
Open Graph Description: Bug Report types.GeneratorType is documented as "The type of generator-iterator objects, created by generator functions." Therefore I would expect that a value annotated using typing.Generator woul...
X Description: Bug Report types.GeneratorType is documented as "The type of generator-iterator objects, created by generator functions." Therefore I would expect that a value annotated using typing.Gene...
Opengraph URL: https://github.com/python/typeshed/issues/5496
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"No Generator is considered to be an instance of GeneratorType","articleBody":"**Bug Report**\r\n\r\n`types.GeneratorType` is [documented as](https://docs.python.org/3/library/types.html#types.GeneratorType) \"The type of generator-iterator objects, created by generator functions.\" Therefore I would expect that a value annotated using `typing.Generator` would be considered an instance of `GeneratorType`. However, that is not the case.\r\n\r\nIt is possible that this issue originates in typeshed instead of mypy; I don't know whether mypy is drawing the wrong conclusions or is operating on flawed definitions. Even if the problem is in typeshed, perhaps mypy can output a more detailed error message, listing which method signatures it considers incompatible.\r\n\r\n**To Reproduce**\r\n\r\nRun mypy on the following code:\r\n\r\n```py\r\nfrom types import GeneratorType\r\nfrom typing import Any, Generator\r\n\r\ndef check(g: Generator[Any, Any, Any]) -> None:\r\n if not isinstance(g, GeneratorType):\r\n raise TypeError(\"Not a generator\")\r\n print(\"It is a generator\")\r\n\r\ndef gen() -> Generator[int, None, None]:\r\n yield 123\r\n\r\ncheck(gen())\r\n\r\ntry:\r\n check(None)\r\nexcept TypeError:\r\n print(\"Non-generator is rejected\")\r\nelse:\r\n print(\"Non-generator is accepted\")\r\n```\r\n\r\n**Expected Behavior**\r\n\r\nOnly `check(None)` should be flagged as an error.\r\n\r\n**Actual Behavior**\r\n\r\n```\r\n$ mypy generator_type.py \r\ngenerator_type.py:5: error: Subclass of \"Generator[Any, Any, Any]\" and \"GeneratorType\" cannot exist: would have incompatible method signatures [unreachable]\r\ngenerator_type.py:7: error: Statement is unreachable [unreachable]\r\ngenerator_type.py:15: error: Argument 1 to \"check\" has incompatible type \"None\"; expected \"Generator[Any, Any, Any]\" [arg-type]\r\nFound 3 errors in 1 file (checked 1 source file)\r\n```\r\n\r\nThe first error (`isinstance`), reported on line 5, is the one I consider a bug.\r\nThe error reported on line 7 (`print`) is a consequence of the one on line 5.\r\nThe error reported on line 15 (`check(None)`) is an actual error.\r\n\r\nNote that the runtime behavior is as expected:\r\n```\r\n$ python generator_type.py \r\nIt is a generator\r\nNon-generator is rejected\r\n```\r\n\r\n**Your Environment**\r\n\r\nI'm using mypy 0.812 with Python 3.8.8 on openSUSE Linux.\r\n","author":{"url":"https://github.com/mthuurne","@type":"Person","name":"mthuurne"},"datePublished":"2021-05-18T19:55:38Z","url":"https://github.com/python/typeshed/issues/5496"}
| 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:50fa97f6-ddbc-889d-1d7a-168d09595844 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E0A0:3FDF0C:9E8C46:D3371B:6B0043A4 |
| html-safe-nonce | 6c8cc603c895c6bf2a224042a2820bc73f7ef4991472122ea32402bbc3033eeb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMEEwOjNGREYwQzo5RThDNDY6RDMzNzFCOjZCMDA0M0E0IiwidmlzaXRvcl9pZCI6IjkwMjczNTE5Mzg4NzM3MDUzODAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ec93349f9d65b7d37c1209a086f9acaf861fd9fdf7d4da6cf538e4df6bb6061b |
| hovercard-subject-tag | issue:894742167 |
| 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/typeshed/5496/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e128184a8bd52baf5e10f1f868b57e88c7cfdccc373a8c059b6034cba2f7212f/python/typeshed/issues/5496 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e128184a8bd52baf5e10f1f868b57e88c7cfdccc373a8c059b6034cba2f7212f/python/typeshed/issues/5496 |
| og:image:alt | Bug Report types.GeneratorType is documented as "The type of generator-iterator objects, created by generator functions." Therefore I would expect that a value annotated using typing.Generator woul... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mthuurne |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-preview |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width