Title: Failure to account for None-returning generators in StopIteration.args · Issue #18073 · python/mypy · GitHub
Open Graph Title: Failure to account for None-returning generators in StopIteration.args · Issue #18073 · python/mypy
X Title: Failure to account for None-returning generators in StopIteration.args · Issue #18073 · python/mypy
Description: In psycopg 3 we have generators returning a final result, which sometimes is None, sometimes a different object. We also have some consumers of these generators, which wait for I/O completion according to the values yielded by the genera...
Open Graph Description: In psycopg 3 we have generators returning a final result, which sometimes is None, sometimes a different object. We also have some consumers of these generators, which wait for I/O completion accor...
X Description: In psycopg 3 we have generators returning a final result, which sometimes is None, sometimes a different object. We also have some consumers of these generators, which wait for I/O completion accor...
Opengraph URL: https://github.com/python/mypy/issues/18073
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Failure to account for None-returning generators in StopIteration.args","articleBody":"In psycopg 3 we have generators returning a final result, which sometimes is None, sometimes a different object. We also have some consumers of these generators, which wait for I/O completion according to the values yielded by the generators and finally return the final generator result.\r\n\r\nSomething similar can be represented with the following code:\r\n\r\n```python\r\nfrom typing import Any, Generator, TypeAlias, TypeVar\r\n\r\nRV = TypeVar(\"RV\")\r\n\r\nPQGen: TypeAlias = Generator[Any, Any, RV]\r\n\r\n\r\ndef str_gen() -> PQGen[str]:\r\n yield 10\r\n return \"abc\"\r\n\r\n\r\ndef none_gen() -> PQGen[None]:\r\n yield 10\r\n return None\r\n\r\n\r\ndef wait(gen: PQGen[RV]) -> RV:\r\n try:\r\n while True:\r\n next(gen)\r\n except StopIteration as ex:\r\n rv: RV = ex.args[0] if ex.args else None\r\n return rv\r\n\r\n\r\nprint(f\"{wait(str_gen())=}\")\r\nprint(f\"{wait(none_gen())=}\")\r\n```\r\n\r\nIf a generator returns `None`, the `StopIteration.args` tuple will empty, which is accounted for in the `wait()`'s return statement (removing the check will cause an IndexError on `wait(none_gen())`). However, since Mypy 1.12, the return statement in the wait returns an error:\r\n\r\n```\r\nbug_mypy.py:24: error: Incompatible types in assignment (expression has type \"Any | None\", variable has type \"RV\") [assignment]\r\n```\r\n\r\nThis wasn't reported as error in Mypy 1.11","author":{"url":"https://github.com/dvarrazzo","@type":"Person","name":"dvarrazzo"},"datePublished":"2024-10-30T13:53:30Z","url":"https://github.com/python/mypy/issues/18073"}
| 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:1c59c39f-c492-1964-af68-165f61c77324 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D4D6:142FA7:19ADEE:22AF88:6B002061 |
| html-safe-nonce | 6c79bd225dd26948f215d16b3342522a40c674be7a5d205f6bd5072d83d1502f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENEQ2OjE0MkZBNzoxOUFERUU6MjJBRjg4OjZCMDAyMDYxIiwidmlzaXRvcl9pZCI6IjEzNjU1ODgyMjUxMDc2OTc3NjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a6cd9ac6d383984474f3cfa8d72e09f06538097cbbc2b3247732e9f1096be11f |
| hovercard-subject-tag | issue:2624186689 |
| 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/18073/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c4473c164443bd5cf73b9527f12d9a3987dc85fd59f2d55585be07a860c1c3df/python/mypy/issues/18073 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c4473c164443bd5cf73b9527f12d9a3987dc85fd59f2d55585be07a860c1c3df/python/mypy/issues/18073 |
| og:image:alt | In psycopg 3 we have generators returning a final result, which sometimes is None, sometimes a different object. We also have some consumers of these generators, which wait for I/O completion accor... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dvarrazzo |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width