Title: two-arg iter() with None sentinel does not remove ‘Optional’ · Issue #3201 · python/typeshed · GitHub
Open Graph Title: two-arg iter() with None sentinel does not remove ‘Optional’ · Issue #3201 · python/typeshed
X Title: two-arg iter() with None sentinel does not remove ‘Optional’ · Issue #3201 · python/typeshed
Description: [moved from https://github.com/python/mypy/issues/7371] The 2-arg form of iter() takes a callable and a sentinel value. Quoting its docstring: iter(callable, sentinel) -> iterator [...], the callable is called until it returns the sentin...
Open Graph Description: [moved from https://github.com/python/mypy/issues/7371] The 2-arg form of iter() takes a callable and a sentinel value. Quoting its docstring: iter(callable, sentinel) -> iterator [...], the callab...
X Description: [moved from https://github.com/python/mypy/issues/7371] The 2-arg form of iter() takes a callable and a sentinel value. Quoting its docstring: iter(callable, sentinel) -> iterator [...], the cal...
Opengraph URL: https://github.com/python/typeshed/issues/3201
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"two-arg iter() with None sentinel does not remove ‘Optional’","articleBody":"[moved from https://github.com/python/mypy/issues/7371]\r\n\r\nThe 2-arg form of `iter()` takes a callable and a sentinel value. Quoting its docstring:\r\n\r\n```\r\niter(callable, sentinel) -> iterator\r\n\r\n[...], the callable is called until it returns the sentinel.\r\n```\r\n\r\nWhen the sentinel is the literal `None`, this means that a callable that returns an `Optional[T]` is transformed into a `Iterator[T]`. However, mypy does not detect that, and `reveal_type` on the `iter()` result claims its `Revealed type is 'typing.Iterator[Union[T, None]]'`, which allows for `None` values, which is impossible.\r\n\r\nHere's a small example:\r\n\r\n```python\r\nfrom typing import Optional\r\n\r\ndef maybe_int() -> Optional[int]:\r\n return 123 # or maybe None\r\n\r\nfor n in iter(maybe_int, None):\r\n print(n + n)\r\n```\r\n\r\nEven though `n` is always an `int` inside the loop (never `None`), mypy (version 0.720) is not aware of that, and complains:\r\n\r\n```\r\nerror: Unsupported operand types for + (\"int\" and \"None\")\r\nerror: Unsupported operand types for + (\"None\" and \"int\")\r\nerror: Unsupported left operand type for + (\"None\")\r\nBoth left and right operands are unions\r\n```\r\n\r\nSince the code is perfectly safe, mypy should infer that `n` is of type `int` (not `Optional[int]`) and should not report any errors about adding `int` and `None` values.\r\n","author":{"url":"https://github.com/wbolster-eiq","@type":"Person","name":"wbolster-eiq"},"datePublished":"2019-08-20T13:59:33Z","url":"https://github.com/python/typeshed/issues/3201"}
| 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:3e0d043f-7823-2a18-6e9d-1774ae29e040 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BD74:194E10:256FE68:33060B3:6B0037A6 |
| html-safe-nonce | 5c7c36b090a6f3a4e66d13b355bbd7b9153c29b02bb8e9a3dadce3b979e3836c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRDc0OjE5NEUxMDoyNTZGRTY4OjMzMDYwQjM6NkIwMDM3QTYiLCJ2aXNpdG9yX2lkIjoiMjM3NzgyNzMyODYwOTc2MTE5MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 836043e7fde62c2d08f931c4a6ae7c636a32516c8ba2d6ebaa9e70b3fb597642 |
| hovercard-subject-tag | issue:482879027 |
| 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/3201/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c7fd90852d3053fd0062b9b395c418ee5efef6e23a43c7f282b065260888bb84/python/typeshed/issues/3201 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c7fd90852d3053fd0062b9b395c418ee5efef6e23a43c7f282b065260888bb84/python/typeshed/issues/3201 |
| og:image:alt | [moved from https://github.com/python/mypy/issues/7371] The 2-arg form of iter() takes a callable and a sentinel value. Quoting its docstring: iter(callable, sentinel) -> iterator [...], the callab... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | wbolster-eiq |
| 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