Title: Add support for simple lambda type guard · Issue #9890 · python/mypy · GitHub
Open Graph Title: Add support for simple lambda type guard · Issue #9890 · python/mypy
X Title: Add support for simple lambda type guard · Issue #9890 · python/mypy
Description: Feature Consider the following code: from typing import Optional, List list_with_none: List[Optional[int]] = [None, 1, 2] filtered = filter(lambda v: v is not None, list_with_none) reveal_type(filtered) # Revealed type is 'typing.Iterato...
Open Graph Description: Feature Consider the following code: from typing import Optional, List list_with_none: List[Optional[int]] = [None, 1, 2] filtered = filter(lambda v: v is not None, list_with_none) reveal_type(filt...
X Description: Feature Consider the following code: from typing import Optional, List list_with_none: List[Optional[int]] = [None, 1, 2] filtered = filter(lambda v: v is not None, list_with_none) reveal_type(filt...
Opengraph URL: https://github.com/python/mypy/issues/9890
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add support for simple lambda type guard","articleBody":"**Feature**\r\n\r\nConsider the following code:\r\n\r\n```python\r\nfrom typing import Optional, List\r\n\r\n\r\nlist_with_none: List[Optional[int]] = [None, 1, 2]\r\n\r\n\r\nfiltered = filter(lambda v: v is not None, list_with_none)\r\nreveal_type(filtered)\r\n# Revealed type is 'typing.Iterator[Union[builtins.int, None]]'\r\n```\r\n\r\nIt would be nice if mypy was able to parse simple lambda functions like this one, and infer that `filtered` is `typing.Iterator[builtins.int]`.\r\n\r\nNote that in this example, we use a simple none check, but this could work with more complicated checks:\r\n\r\n```python\r\nlist_of_tuples: List[Tuple[Optional[int], Optional[int]]]\r\nfilter(lambda v: v[0] is not None and v[1] is not None, list_of_tuples)\r\n# would become `Iterator[Tuple[int, int]]`\r\n```\r\n\r\n**Pitch**\r\n\r\nThis is a fairly common idiom in Python.\r\n\r\nReasons why mypy might now want to do this:\r\n\r\n- Supporting simple lambda is ok, but there will inevitably be a limit to what mypy can support\r\n- We could use https://www.python.org/dev/peps/pep-0647/ to achieve the same thing without using lambdas","author":{"url":"https://github.com/charlax","@type":"Person","name":"charlax"},"datePublished":"2021-01-08T16:28:55Z","url":"https://github.com/python/mypy/issues/9890"}
| 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:af530ea3-74fe-58bf-7b5a-358fc6a375a1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D05E:2F0104:1EA110E:28AA894:6B0048B6 |
| html-safe-nonce | a875463d9b7d30b834065ba3cefb7530cd230e8cc57ea779752322230290bfa1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMDVFOjJGMDEwNDoxRUExMTBFOjI4QUE4OTQ6NkIwMDQ4QjYiLCJ2aXNpdG9yX2lkIjoiMzQxNDE2MTU1MDgyOTcwMTMwMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 847fdf76a1fa3652eaf3b180ba7ed088c92de3a5d6e067fbffa0293933664c5b |
| hovercard-subject-tag | issue:782240738 |
| 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/9890/issue_layout |
| twitter:image | https://opengraph.githubassets.com/be496f8c08b70a1e3ca09603b5e4029b2cd3bff61439161da678720cc94ccce8/python/mypy/issues/9890 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/be496f8c08b70a1e3ca09603b5e4029b2cd3bff61439161da678720cc94ccce8/python/mypy/issues/9890 |
| og:image:alt | Feature Consider the following code: from typing import Optional, List list_with_none: List[Optional[int]] = [None, 1, 2] filtered = filter(lambda v: v is not None, list_with_none) reveal_type(filt... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | charlax |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width