Title: Exhaustive Matching on Enums / Literals · Issue #6366 · python/mypy · GitHub
Open Graph Title: Exhaustive Matching on Enums / Literals · Issue #6366 · python/mypy
X Title: Exhaustive Matching on Enums / Literals · Issue #6366 · python/mypy
Description: Are you reporting a bug, or opening a feature request? Asking a question. I'd like to know if there is a standard idiom for enforcing exhaustive matching of an Enum / Literal type. Please insert below the code you are checking with mypy ...
Open Graph Description: Are you reporting a bug, or opening a feature request? Asking a question. I'd like to know if there is a standard idiom for enforcing exhaustive matching of an Enum / Literal type. Please insert be...
X Description: Are you reporting a bug, or opening a feature request? Asking a question. I'd like to know if there is a standard idiom for enforcing exhaustive matching of an Enum / Literal type. Please inser...
Opengraph URL: https://github.com/python/mypy/issues/6366
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Exhaustive Matching on Enums / Literals","articleBody":"* Are you reporting a bug, or opening a feature request?\r\n\r\nAsking a question. I'd like to know if there is a standard idiom for enforcing exhaustive matching of an Enum / Literal type.\r\n\r\n* Please insert below the code you are checking with mypy\r\n\r\nSuppose there are three types of pets: cats, dogs and iguanas. I want to write a function that makes the appropriate pet sound, depending on the type of pet. I'd also like to make sure that I've caught every possible type of pet. Here was my first attempt, using the `NoReturn` trick mentioned by @bluetech [here](https://github.com/python/mypy/issues/5818). It looks like this works for Unions, but not for Enums / Literals? Apologies if this has been covered before.\r\n\r\n```\r\ndef assert_never(x: Any) -> NoReturn:\r\n assert False, \"Unhandled type: {}\".format(type(x).__name__)\r\n\r\nPetLiteral = Literal['dog', 'cat', 'iguana']\r\n\r\ndef make_sound_literal(pet: PetLiteral) -> str:\r\n if pet == 'dog':\r\n return 'woof'\r\n elif pet == 'cat':\r\n return 'meow'\r\n # elif pet == 'iguana':\r\n # return 'confused silence'\r\n else:\r\n assert_never(pet)\r\n```\r\n\r\nIf I comment out the iguana branch and check this file with:\r\n\r\n```\r\n$ mypy --strict pet_sounds.py\r\n```\r\nmypy has no complaints. If I try the same deal with an enum:\r\n\r\n```\r\nclass PetEnum(Enum):\r\n dog: str = 'dog'\r\n cat: str = 'cat'\r\n iguana: str = 'iguana'\r\n\r\ndef make_sound_enum(pet: PetEnum) -> str:\r\n if pet == PetEnum.dog:\r\n return 'woof'\r\n elif pet == PetEnum.cat:\r\n return 'meow'\r\n# elif pet == PetEnum.iguana:\r\n# return 'confused silence'\r\n else:\r\n assert_never(pet)\r\n```\r\nand check again with `--strict`, I still get no complaints. \r\n\r\n* What are the versions of mypy and Python you are using?\r\nmypy 0.660\r\nPython 3.6.5\r\n\r\n* Do you see the same issue after installing mypy from Git master?\r\n\r\nYes, got the same behavior with `mypy 0.680+dev.21c8a812c697baf7394eafe360188ededcec6d9c`\r\n\r\n* What are the mypy flags you are using? (For example --strict-optional)\r\n`--strict`\r\n","author":{"url":"https://github.com/poneill","@type":"Person","name":"poneill"},"datePublished":"2019-02-09T03:05:04Z","url":"https://github.com/python/mypy/issues/6366"}
| 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:14ff89b2-f32e-c63c-9ce9-99d362ff941c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E742:3430F7:9ACE5D:CF7DB7:6B0043C2 |
| html-safe-nonce | e9381a49a7f69f94cf35a35360b5d100ae0627e1eec965e726dd8ea9d58ae958 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNzQyOjM0MzBGNzo5QUNFNUQ6Q0Y3REI3OjZCMDA0M0MyIiwidmlzaXRvcl9pZCI6IjQ5MTE2MDQ1NTcxNjQ4NTYyNTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a2a639cd78815dda2a3c03c3199e5f927ddb6f05f6220f074b15178679b9337d |
| hovercard-subject-tag | issue:408391467 |
| 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/6366/issue_layout |
| twitter:image | https://opengraph.githubassets.com/da311b20d4fadfa65f6c76b07066bb52490ce124281ff69fbc21c1d0c0a0db04/python/mypy/issues/6366 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/da311b20d4fadfa65f6c76b07066bb52490ce124281ff69fbc21c1d0c0a0db04/python/mypy/issues/6366 |
| og:image:alt | Are you reporting a bug, or opening a feature request? Asking a question. I'd like to know if there is a standard idiom for enforcing exhaustive matching of an Enum / Literal type. Please insert be... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | poneill |
| 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