René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:14ff89b2-f32e-c63c-9ce9-99d362ff941c
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idE742:3430F7:9ACE5D:CF7DB7:6B0043C2
html-safe-noncee9381a49a7f69f94cf35a35360b5d100ae0627e1eec965e726dd8ea9d58ae958
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNzQyOjM0MzBGNzo5QUNFNUQ6Q0Y3REI3OjZCMDA0M0MyIiwidmlzaXRvcl9pZCI6IjQ5MTE2MDQ1NTcxNjQ4NTYyNTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmaca2a639cd78815dda2a3c03c3199e5f927ddb6f05f6220f074b15178679b9337d
hovercard-subject-tagissue:408391467
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/6366/issue_layout
twitter:imagehttps://opengraph.githubassets.com/da311b20d4fadfa65f6c76b07066bb52490ce124281ff69fbc21c1d0c0a0db04/python/mypy/issues/6366
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/da311b20d4fadfa65f6c76b07066bb52490ce124281ff69fbc21c1d0c0a0db04/python/mypy/issues/6366
og:image:altAre 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameponeill
hostnamegithub.com
expected-hostnamegithub.com
None7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0
turbo-cache-controlno-preview
go-importgithub.com/python/mypy git https://github.com/python/mypy.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id7053637
octolytics-dimension-repository_nwopython/mypy
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id7053637
octolytics-dimension-repository_network_root_nwopython/mypy
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasef3f3a43ca095cdea3abbb394112e971f9fdb94fa
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/mypy/issues/6366#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F6366
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F6366
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fmypy
Reloadhttps://github.com/python/mypy/issues/6366
Reloadhttps://github.com/python/mypy/issues/6366
Reloadhttps://github.com/python/mypy/issues/6366
pythonhttps://github.com/python
mypyhttps://github.com/python/mypy
Codehttps://github.com/python/mypy
Issues2.7k (2.7k)https://github.com/python/mypy/issues
Pull requests545 (545)https://github.com/python/mypy/pulls
Actionshttps://github.com/python/mypy/actions
Projectshttps://github.com/python/mypy/projects
Wikihttps://github.com/python/mypy/wiki
Security and qualityhttps://github.com/python/mypy/security
Insightshttps://github.com/python/mypy/insights
https://github.com/python/mypy
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/6366
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/6366
Exhaustive Matching on Enums / Literalshttps://github.com/python/mypy/issues/6366#top
false-positivemypy gave an error on correct codehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22false-positive%22
featurehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22feature%22
priority-1-normalhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22priority-1-normal%22
topic-enumhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-enum%22
topic-literal-typeshttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-literal-types%22
https://github.com/poneill
poneillhttps://github.com/poneill
on Feb 9, 2019https://github.com/python/mypy/issues/6366#issue-408391467
@bluetechhttps://github.com/bluetech
herehttps://github.com/python/mypy/issues/5818
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Feb 9, 2019https://github.com/python/mypy/issues/6366#issuecomment-462039054
#5935https://github.com/python/mypy/issues/5935
@Michael0x2ahttps://github.com/Michael0x2a
ilevkivskyihttps://github.com/ilevkivskyi
featurehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22feature%22
priority-1-normalhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22priority-1-normal%22
topic-literal-typeshttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-literal-types%22
on Feb 9, 2019https://github.com/python/mypy/issues/6366#event-2128707982
Michael0x2ahttps://github.com/Michael0x2a
on Jun 16, 2019https://github.com/python/mypy/issues/6366#event-2415765335
Generalize reachability checks to support enumshttps://github.com/Michael0x2a/mypy/commit/df76db6b1b00078aeae9e81d6ad9fe512313b20a
df76db6https://github.com/Michael0x2a/mypy/commit/df76db6b1b00078aeae9e81d6ad9fe512313b20a
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Jul 6, 2019https://github.com/python/mypy/issues/6366#issuecomment-508947129
#4223https://github.com/python/mypy/issues/4223
ilevkivskyihttps://github.com/ilevkivskyi
false-positivemypy gave an error on correct codehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22false-positive%22
on Jul 6, 2019https://github.com/python/mypy/issues/6366#event-2464065561
ilevkivskyihttps://github.com/ilevkivskyi
on Jul 8, 2019https://github.com/python/mypy/issues/6366#event-2466543893
Generalize reachability checks to support enums (#7000)https://github.com/python/mypy/commit/e818a96917a5111c4a5b901d49aa6b36de9700d3
#7000https://github.com/python/mypy/pull/7000
e818a96https://github.com/python/mypy/commit/e818a96917a5111c4a5b901d49aa6b36de9700d3
https://github.com/davidshepherd7
davidshepherd7https://github.com/davidshepherd7
on Dec 2, 2019https://github.com/python/mypy/issues/6366#issuecomment-560369716
https://github.com/ezyang
ezyanghttps://github.com/ezyang
on Aug 5, 2020https://github.com/python/mypy/issues/6366#issuecomment-669382999
https://github.com/ezyang
ezyanghttps://github.com/ezyang
on Aug 14, 2020https://github.com/python/mypy/issues/6366#issuecomment-673876383
https://github.com/sbdchd
sbdchdhttps://github.com/sbdchd
on Dec 18, 2020https://github.com/python/mypy/issues/6366#issuecomment-747831749
https://docs.python.org/3.8/library/enum.html#comparisonshttps://docs.python.org/3.8/library/enum.html#comparisons
https://github.com/sbdchd
sbdchdhttps://github.com/sbdchd
on Jan 29, 2021https://github.com/python/mypy/issues/6366#issuecomment-769566975
mypy/test-data/unit/check-narrowing.testhttps://github.com/python/mypy/blob/4d5a1bc75224e1c532557861261becac7d8fdf47/test-data/unit/check-narrowing.test#L783-L802
4d5a1bchttps://github.com/python/mypy/commit/4d5a1bc75224e1c532557861261becac7d8fdf47
mypy/test-data/unit/check-narrowing.testhttps://github.com/python/mypy/blob/4d5a1bc75224e1c532557861261becac7d8fdf47/test-data/unit/check-narrowing.test#L681-L710
4d5a1bchttps://github.com/python/mypy/commit/4d5a1bc75224e1c532557861261becac7d8fdf47
3dce3fdhttps://github.com/python/mypy/commit/3dce3fd18e3e48c1e892951950865ea97f4ee4ed
sbdchdhttps://github.com/sbdchd
on Feb 13, 2021https://github.com/python/mypy/issues/6366#event-4327329258
ref(bot): use exhaustiveness checking via NoReturn typehttps://github.com/chdsbd/kodiak/commit/44e96159281465cbf3a421d2fbb8cbba2efa8780
44e9615https://github.com/chdsbd/kodiak/commit/44e96159281465cbf3a421d2fbb8cbba2efa8780
kodiakhqhttps://github.com/apps/kodiakhq
on Feb 13, 2021https://github.com/python/mypy/issues/6366#event-4327355743
ref(bot): use exhaustiveness checking via NoReturn type (#614)https://github.com/chdsbd/kodiak/commit/4d4c2a31b6ceb2136f83281a154f47e8b49a86ac
#614https://github.com/chdsbd/kodiak/pull/614
4d4c2a3https://github.com/chdsbd/kodiak/commit/4d4c2a31b6ceb2136f83281a154f47e8b49a86ac
ilevkivskyihttps://github.com/ilevkivskyi
on Jul 23, 2021https://github.com/python/mypy/issues/6366#event-5058778123
Adds docs about exhaustive literal and enum checks (#10860)https://github.com/python/mypy/commit/fb75cdc352c8a6da723800619fe05b4ecb4f6cf4
#10860https://github.com/python/mypy/pull/10860
fb75cdchttps://github.com/python/mypy/commit/fb75cdc352c8a6da723800619fe05b4ecb4f6cf4
https://github.com/erictraut
erictrauthttps://github.com/erictraut
on Dec 31, 2021https://github.com/python/mypy/issues/6366#issuecomment-1003446602
https://github.com/bbatliner
bbatlinerhttps://github.com/bbatliner
on Feb 17, 2022https://github.com/python/mypy/issues/6366#issuecomment-1043295495
https://github.com/bbatliner
bbatlinerhttps://github.com/bbatliner
on Feb 17, 2022https://github.com/python/mypy/issues/6366#issuecomment-1043302655
https://github.com/JelleZijlstra
JelleZijlstrahttps://github.com/JelleZijlstra
on Feb 17, 2022https://github.com/python/mypy/issues/6366#issuecomment-1043306584
https://github.com/bbatliner
bbatlinerhttps://github.com/bbatliner
on Feb 17, 2022https://github.com/python/mypy/issues/6366#issuecomment-1043350779
AlexWaygoodhttps://github.com/AlexWaygood
topic-enumhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-enum%22
on Mar 24, 2022https://github.com/python/mypy/issues/6366#event-6296865412
https://github.com/hauntsaninja
hauntsaninjahttps://github.com/hauntsaninja
on Apr 24, 2023https://github.com/python/mypy/issues/6366#issuecomment-1519488009
#11521https://github.com/python/mypy/pull/11521
hauntsaninjahttps://github.com/hauntsaninja
completedhttps://github.com/python/mypy/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Apr 24, 2023https://github.com/python/mypy/issues/6366#event-9077525498
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/mypy/issues/6366
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/mypy/issues/6366
false-positivemypy gave an error on correct codehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22false-positive%22
featurehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22feature%22
priority-1-normalhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22priority-1-normal%22
topic-enumhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-enum%22
topic-literal-typeshttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-literal-types%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.