Title: Allow yielding result of function that returns None · Issue #1933 · python/mypy · GitHub
Open Graph Title: Allow yielding result of function that returns None · Issue #1933 · python/mypy
X Title: Allow yielding result of function that returns None · Issue #1933 · python/mypy
Description: UPDATE: see below for current status #1933 (comment) This is a spin-off from PR #1808. @ddfisher and I cannot agree on what the default type should be in the three methods get_generator_yield_type(), get_generator_receive_type(), get_gen...
Open Graph Description: UPDATE: see below for current status #1933 (comment) This is a spin-off from PR #1808. @ddfisher and I cannot agree on what the default type should be in the three methods get_generator_yield_type(...
X Description: UPDATE: see below for current status #1933 (comment) This is a spin-off from PR #1808. @ddfisher and I cannot agree on what the default type should be in the three methods get_generator_yield_type(...
Opengraph URL: https://github.com/python/mypy/issues/1933
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow yielding result of function that returns None","articleBody":"_UPDATE:_ see below for current status https://github.com/python/mypy/issues/1933#issuecomment-390018636\r\n\r\nThis is a spin-off from PR #1808. @ddfisher and I cannot agree on what the default type should be in the three methods `get_generator_yield_type()`, `get_generator_receive_type()`, `get_generator_return_type()`.\r\n\r\nThe setting: when the user defines a generator function, they can specify a return type. The return type is required to be a _supertype_ of `Generator`; this constrains it to `Iterator`, `Iterable`, `object` (and `Any`, which is special in other ways).\r\n\r\nThe `Generator` type has _three_ type parameters:\r\n- `ty`, the yield type: the type of `y` in `yield y`\r\n- `tc`, the receive type: the type of `c` in `c = yield ...`\r\n- `tr`, the return type: the type of `r` in `return r`\r\n\r\nNote that `tr` is fairly new -- it was introduced in Python 3.3 by PEP 380 for the benefit of `yield from`. `tc` is older, it dates back to Python 2.5 and PEP 342 (it is the value sent to the generator using the `.send()` method). `ty` is the oldest; when `yield` was originally introduced in Python 2.2 by PEP 255, it was a statement. Before PEP 380, `return` in a generator function was not allowed to return a value.\r\n\r\nWhen a generator function is just intended to be used as an iterator, there's no need to specify `tc` and `tr`, and the recommended type is `Iterator[ty]`. (`Iterable[ty]` is also allowed and means roughly the same thing.) In this case the user of the iterator has no access to the `.send()` method. Hence, `yield` might as well be considered a statement, since it's never going to return anything other than `None`. Likewise, there's no point is returning a value with a `return` statement since the user of the iterator in all likelihood is just going to iterate over it until it raises `StopIteration` (since that's all that regular iterators do).\r\n\r\nThis is where @ddfisher and I disagree -- I find it useful if the defaults for `tc` and `tr` are None (or perhaps Void), so that mypy will flag an error in the generator body if you either try to use `yield ...` as an expression, or try to return a value using `return \u003cexpr>`. David (IIUC) believes that both should be `Any`, giving the reason that \"it should be allow to pointless but harmless things.\" (The code currently has [`tc` default to `Void`](https://github.com/python/mypy/blob/master/mypy/checker.py#L298) and [`tr` default to `Any`](https://github.com/python/mypy/blob/master/mypy/checker.py#L317). Go figure.)\r\n\r\nMy problem with David's position is that I want mypy to be strict about pointless things that are likely the result of either a misunderstanding or a refactoring gone bad. I have seen enough people make the mistake of accidentally writing `return v` instead of `yield v` in a generator that I want this to be flagged as an error. There's also the somewhat analogous case of a regular function declared to return `None` -- it would be pointless but harmless to return some value here, so maybe David believes that we shouldn't flag that as an error either?\r\n\r\nI'm not sure why the existing code (written by David) has `Void` as the default for `tc`, but I think it may be because this parameter of `Generator` is [contravariant](https://github.com/python/typeshed/blob/master/stdlib/3/typing.pyi#L99). So hopefully we at least agree here. :-)\r\n\r\nDavid, please feel free to explain your position more fully (I think you were calling on the formal type system but we ran out of time in our in-person discussion at that point).\r\n","author":{"url":"https://github.com/gvanrossum","@type":"Person","name":"gvanrossum"},"datePublished":"2016-07-23T20:57:38Z","url":"https://github.com/python/mypy/issues/1933"}
| 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:d3aa759f-3d05-f831-3491-3438088aed80 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A744:1A1C04:2102F88:2D03D29:6B0022CD |
| html-safe-nonce | d96dff570428dc0d63682bfa414b9ced51c7bf5c41a1b9cc417cc5fe6c3d7e41 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzQ0OjFBMUMwNDoyMTAyRjg4OjJEMDNEMjk6NkIwMDIyQ0QiLCJ2aXNpdG9yX2lkIjoiMjczNzM1MjE5ODA1MTY4MzMzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | ead7b063e282ce95dc43d4cc414b6f9ab666f1bb1651e1093eec20a7d19c9785 |
| hovercard-subject-tag | issue:167203179 |
| 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/1933/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a0f958a7a3e865f1cd64cb1a38e2a510927f03152841f0f80c1ad132266facda/python/mypy/issues/1933 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a0f958a7a3e865f1cd64cb1a38e2a510927f03152841f0f80c1ad132266facda/python/mypy/issues/1933 |
| og:image:alt | UPDATE: see below for current status #1933 (comment) This is a spin-off from PR #1808. @ddfisher and I cannot agree on what the default type should be in the three methods get_generator_yield_type(... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gvanrossum |
| 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