Title: Narrow types after 'in' operator. · Issue #4071 · python/mypy · GitHub
Open Graph Title: Narrow types after 'in' operator. · Issue #4071 · python/mypy
X Title: Narrow types after 'in' operator. · Issue #4071 · python/mypy
Description: Sometimes it is useful to narrow down a type after an in/not in comparison. For example: class Option: ... opt: Union[Option, str] if opt in ('verbose', 'silent'): reveal_type(opt) # we know this is 'str' else: ... # still a union here p...
Open Graph Description: Sometimes it is useful to narrow down a type after an in/not in comparison. For example: class Option: ... opt: Union[Option, str] if opt in ('verbose', 'silent'): reveal_type(opt) # we know this i...
X Description: Sometimes it is useful to narrow down a type after an in/not in comparison. For example: class Option: ... opt: Union[Option, str] if opt in ('verbose', 'silent'): reveal_type(opt) ...
Opengraph URL: https://github.com/python/mypy/issues/4071
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Narrow types after 'in' operator.","articleBody":"Sometimes it is useful to narrow down a type after an ``in``/``not in`` comparison. For example:\r\n```python\r\nclass Option:\r\n ...\r\nopt: Union[Option, str]\r\nif opt in ('verbose', 'silent'):\r\n reveal_type(opt) # we know this is 'str'\r\nelse:\r\n ... # still a union here\r\n\r\npresets: List[Option]\r\nif opt not in presets:\r\n ... # still a union\r\nelse:\r\n reveal_type(opt) # we know this can be only 'Option'\r\n```\r\nThis seems to be especially useful for ``--strict-optional``, for example:\r\n```python\r\nclass Node:\r\n fullname: Optional[str]\r\nn: Node\r\n\r\nreverse_aliases: Dict[str, str]\r\nif n.fullname in reverse_aliases:\r\n reveal_type(n.fullname) # This should be 'str'\r\n```\r\n(related https://github.com/python/mypy/issues/2980)\r\n\r\nA similar idea was discussed in https://github.com/python/mypy/issues/1749, but emitting an error on ``if 'a' in [1, 2, 3]: ...`` would require a larger change: emitting errors on unreachable code in binder.","author":{"url":"https://github.com/ilevkivskyi","@type":"Person","name":"ilevkivskyi"},"datePublished":"2017-10-08T09:11:37Z","url":"https://github.com/python/mypy/issues/4071"}
| 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:579557b4-fa37-f039-df4e-68f48101d33c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E30E:3E7030:2776A9:35208A:6B0024ED |
| html-safe-nonce | 2ab9ed8bee9fcbda9eff91e381d852dc4dafdb083551079a446c9730fc5ba874 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMzBFOjNFNzAzMDoyNzc2QTk6MzUyMDhBOjZCMDAyNEVEIiwidmlzaXRvcl9pZCI6IjQyMzk5ODI0OTUxMTE1ODcwNTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 43748b3d6cb5654c7dcf6772105d66c56850ddc87f1010617d371a7b60361aec |
| hovercard-subject-tag | issue:263704223 |
| 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/4071/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8c25e1218c18d983c15e88414610ba7f6c02cd2cdced9f347b14658246cd00a3/python/mypy/issues/4071 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8c25e1218c18d983c15e88414610ba7f6c02cd2cdced9f347b14658246cd00a3/python/mypy/issues/4071 |
| og:image:alt | Sometimes it is useful to narrow down a type after an in/not in comparison. For example: class Option: ... opt: Union[Option, str] if opt in ('verbose', 'silent'): reveal_type(opt) # we know this i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ilevkivskyi |
| 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