Title: Support enum type narrowing with `in` · Issue #13684 · python/mypy · GitHub
Open Graph Title: Support enum type narrowing with `in` · Issue #13684 · python/mypy
X Title: Support enum type narrowing with `in` · Issue #13684 · python/mypy
Description: The syntax some_enum_value in [list_of_values] should narrow the enum to that list of values. This is a list extension to #10915 "support narrowing enum values using == and !=" Unlike the == case, there is no equivalent is syntax for lis...
Open Graph Description: The syntax some_enum_value in [list_of_values] should narrow the enum to that list of values. This is a list extension to #10915 "support narrowing enum values using == and !=" Unlike the == case, ...
X Description: The syntax some_enum_value in [list_of_values] should narrow the enum to that list of values. This is a list extension to #10915 "support narrowing enum values using == and !=" Unlike the...
Opengraph URL: https://github.com/python/mypy/issues/13684
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support enum type narrowing with `in`","articleBody":"The syntax `some_enum_value in [list_of_values]` should narrow the enum to that list of values.\r\n\r\nThis is a list extension to https://github.com/python/mypy/issues/10915 \"support narrowing enum values using == and !=\"\r\nUnlike the == case, there is no equivalent `is` syntax for lists that will narrow the type.\r\n\r\nThis example shows the motivation for this feature, as well as the syntax that is expected to work.\r\n\r\n```\r\nfrom typing import Literal, Union, cast\r\nfrom enum import Enum\r\n\r\n\r\nclass SomeType(Enum):\r\n a = 1\r\n b = 2\r\n c = 3\r\n\r\n\r\nLimitType = Union[Literal[SomeType.a, SomeType.b]]\r\nAllowedLimitType = [SomeType.a, SomeType.b]\r\n\r\n\r\ndef inner_check(limit: LimitType) -> None:\r\n pass\r\n\r\n\r\ndef outer_check(some: SomeType) -> None:\r\n if some in [SomeType.a, SomeType.b]:\r\n inner_check(some) # Incompatible type\r\n\r\n if some in AllowedLimitType:\r\n inner_check(some) # Incompatible type\r\n\r\n if some == SomeType.a or some == SomeType.b:\r\n inner_check(some) # Incompatible type\r\n inner_check(cast(LimitType, some))\r\n\r\n if some is SomeType.a or some is SomeType.b:\r\n inner_check(some)\r\n\r\n if some == SomeType.a:\r\n inner_check(SomeType.a)\r\n\r\n # This would be ideal\r\n # if some in LimitType:\r\n # inner_check(some)\r\n\r\ndef main() -> None:\r\n outer_check(SomeType.a)\r\n\r\n\r\nmain()\r\n```\r\n","author":{"url":"https://github.com/edaqa-uncountable","@type":"Person","name":"edaqa-uncountable"},"datePublished":"2022-09-19T08:04:13Z","url":"https://github.com/python/mypy/issues/13684"}
| 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:65a3d495-0d3a-995a-1eb6-e65752af5f9d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B3DA:2DCAE8:21159E4:2D0C030:6B002430 |
| html-safe-nonce | b044a4de9f1654e30822e4a20d9845dfe53022ce72960e0a32a64521c607f69b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCM0RBOjJEQ0FFODoyMTE1OUU0OjJEMEMwMzA6NkIwMDI0MzAiLCJ2aXNpdG9yX2lkIjoiNTIyNTI3ODg3NjkwMDQ4NTYwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 840217c96076a6db2ed4a48604511dc43974c1104ef734db9e78d1c6900b4d0a |
| hovercard-subject-tag | issue:1377563332 |
| 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/13684/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5b63dfe49f6cd45e99fb62a8da768dfaa88dc57e659a42887d39726522415641/python/mypy/issues/13684 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5b63dfe49f6cd45e99fb62a8da768dfaa88dc57e659a42887d39726522415641/python/mypy/issues/13684 |
| og:image:alt | The syntax some_enum_value in [list_of_values] should narrow the enum to that list of values. This is a list extension to #10915 "support narrowing enum values using == and !=" Unlike the == case, ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | edaqa-uncountable |
| 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