Title: `==` Narrows the type of an Enum in an if-elif block when following an `is` · Issue #9786 · python/mypy · GitHub
Open Graph Title: `==` Narrows the type of an Enum in an if-elif block when following an `is` · Issue #9786 · python/mypy
X Title: `==` Narrows the type of an Enum in an if-elif block when following an `is` · Issue #9786 · python/mypy
Description: is and == work strangely together when narrowing the type of an Enum in an if-elif block. It is known that you need to use is rather than == to narrow the type of an Enum, but it seems if the if statement uses is then the elif can have =...
Open Graph Description: is and == work strangely together when narrowing the type of an Enum in an if-elif block. It is known that you need to use is rather than == to narrow the type of an Enum, but it seems if the if st...
X Description: is and == work strangely together when narrowing the type of an Enum in an if-elif block. It is known that you need to use is rather than == to narrow the type of an Enum, but it seems if the if st...
Opengraph URL: https://github.com/python/mypy/issues/9786
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`==` Narrows the type of an Enum in an if-elif block when following an `is`","articleBody":"\u003c!--\r\n If you're new to mypy and you're not sure whether what you're experiencing is a mypy bug, please see the \"Question and Help\" form\r\n instead.\r\n-->\r\n\r\n`is` and `==` work strangely together when narrowing the type of an `Enum` in an if-elif block. [It is known](https://github.com/python/mypy/issues/6366#issuecomment-673876383) that you need to use `is` rather than `==` to narrow the type of an `Enum`, but it seems if the `if` statement uses `is` then the `elif` can have `==` and still narrow the type of the variable.\r\n\r\n**To Reproduce**\r\n```\r\nfrom enum import Enum\r\n\r\nclass Foo(Enum):\r\n A = 1\r\n B = 2\r\n C = 3\r\n\r\ndef f0(x: Foo) -> str:\r\n if x is Foo.A:\r\n return 'one'\r\n elif x == Foo.B:\r\n return 'two'\r\n elif x == Foo.C:\r\n return 'three'\r\n reveal_type(x) # Not revealed by mypy as it apparently considers this line unreachable\r\n```\r\nThis is compared to how have come to expect `==` to work with an enum where it will not narrow the type.\r\n\r\n```\r\ndef f1(x: Foo) -> str:\r\n if x == Foo.A:\r\n return 'one'\r\n elif x == Foo.B:\r\n return 'two'\r\n elif x == Foo.C:\r\n return 'three'\r\n reveal_type(x) # Revealed type is Foo\r\n```\r\nInterestingly this doesn't just occur when an `is` is used in the `if`. It can occur in an `elif` and still \"propogate\" its narrowing ability to the following `==`'s.\r\n```\r\ndef f2(x: Foo) -> str:\r\n if x == Foo.A:\r\n return 'one'\r\n elif x is Foo.B:\r\n return 'two'\r\n elif x == Foo.C:\r\n return 'three'\r\n reveal_type(x) # Revealed type is Literal[Foo.A]\r\n```\r\n\r\n**Expected Behavior**\r\n\r\n\r\n`==` would either narrow the type of an enum or not and it would not depend on whether or not it was preceded by an `is`.\r\n\r\n**Actual Behavior**\r\n\r\n`==` narrows the type of an enum only when it is preceded by an `is`.\r\n\r\n**Your Environment**\r\n\r\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\r\n\r\n- Mypy version used: 0.790\r\n- Mypy command-line flags: none or `--strict` will both demonstrate this behavior\r\n- Mypy configuration options from `mypy.ini` (and other config files):\r\n```\r\n[mypy]\r\ndisallow_any_decorated = True\r\ndisallow_any_explicit = True\r\ndisallow_any_generics = True\r\ndisallow_subclassing_any = True\r\ndisallow_untyped_defs = True\r\ndisallow_untyped_calls = True\r\ndisallow_incomplete_defs = True\r\ndisallow_untyped_decorators = True\r\n```\r\n- Python version used: 3.9.0\r\n- Operating system and version: Ubuntu 20.04.1 LTS under WSL\r\n\r\n","author":{"url":"https://github.com/parsons-kyle-89","@type":"Person","name":"parsons-kyle-89"},"datePublished":"2020-12-09T00:19:13Z","url":"https://github.com/python/mypy/issues/9786"}
| 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:9aebfaf4-8cb9-879b-b0e1-2d0b3566cf51 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B786:3CBF1A:25EB0A6:340ACDD:6B0047ED |
| html-safe-nonce | b258a873875d2cfa5e03baa458987c86002e839e52ce2e8165e05c0fb1bbb2ed |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNzg2OjNDQkYxQToyNUVCMEE2OjM0MEFDREQ6NkIwMDQ3RUQiLCJ2aXNpdG9yX2lkIjoiNTE2Nzk5Njg0NzU1NzQ2MTk5NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e1662032b90a47e0094fdc3b4180020e87855b61020f73431621a206cdb5935b |
| hovercard-subject-tag | issue:759885218 |
| 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/9786/issue_layout |
| twitter:image | https://opengraph.githubassets.com/747e033e044a6aef83c7c7b8c43e937e4805bc9fc66111d903961bf8bc800e07/python/mypy/issues/9786 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/747e033e044a6aef83c7c7b8c43e937e4805bc9fc66111d903961bf8bc800e07/python/mypy/issues/9786 |
| og:image:alt | is and == work strangely together when narrowing the type of an Enum in an if-elif block. It is known that you need to use is rather than == to narrow the type of an Enum, but it seems if the if st... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | parsons-kyle-89 |
| 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