Title: Enum `@member` and `nonmember` mishandled in `Literal` types · Issue #18719 · python/mypy · GitHub
Open Graph Title: Enum `@member` and `nonmember` mishandled in `Literal` types · Issue #18719 · python/mypy
X Title: Enum `@member` and `nonmember` mishandled in `Literal` types · Issue #18719 · python/mypy
Description: Bug Report typing.Literal is supposed to accept enum members and not non-members (such as methods or explicit nonmember attributes). mypy fails to enforce the latter and support the former. To Reproduce from enum import Enum, member, non...
Open Graph Description: Bug Report typing.Literal is supposed to accept enum members and not non-members (such as methods or explicit nonmember attributes). mypy fails to enforce the latter and support the former. To Repr...
X Description: Bug Report typing.Literal is supposed to accept enum members and not non-members (such as methods or explicit nonmember attributes). mypy fails to enforce the latter and support the former. To Repr...
Opengraph URL: https://github.com/python/mypy/issues/18719
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Enum `@member` and `nonmember` mishandled in `Literal` types","articleBody":"**Bug Report**\n\n`typing.Literal` is supposed to accept enum members and not non-members (such as methods or explicit `nonmember` attributes). `mypy` fails to enforce the latter and support the former.\n\n**To Reproduce**\n\n```python\nfrom enum import Enum, member, nonmember\nfrom typing import Literal\n\nclass E(Enum):\n A = 1\n @member\n def B() -> None: ...\n C = member(lambda: None)\n D = nonmember(1)\n \n def meth(self) -> None: ...\n \na: Literal[E.A] # OK, true negative\nb: Literal[E.B] # False positive: Parameter 1 of Literal[...] is invalid [valid-type]\nc: Literal[E.C] # OK, true negative\nd: Literal[E.D] # False negative - `nonmember` shouldn't be allowed\nmeth: Literal[E.meth] # OK, true positive: Parameter 1 of Literal[...] is invalid [valid-type]\n```\n\n[playground](https://mypy-play.net/?mypy=latest&python=3.12&flags=strict&gist=826db3d3c8aeba4346fd4220c10097b3)\n\n**Expected Behavior**\n\n* The following should be accepted in Literal: `E.A`, `E.B`, `E.C` \n* The following should be rejected in Literal: `E.D`, `E.meth`\n\n**Actual Behavior**\n\n(line 7 is a different issue reported separately)\n\n```\nmain.py:7: error: Method must have at least one argument. Did you forget the \"self\" argument? [misc]\nmain.py:14: error: Parameter 1 of Literal[...] is invalid [valid-type]\nmain.py:17: error: Parameter 1 of Literal[...] is invalid [valid-type]\n```\n\n**Your Environment**\n\n- Mypy version used: 1.15.0 and current master\n- Mypy command-line flags: N/A\n- Mypy configuration options from `mypy.ini` (and other config files): N/A\n- Python version used: 3.12\n\n\u003c!-- You can freely edit this text, please remove all the lines you believe are unnecessary. -->\n","author":{"url":"https://github.com/sterliakov","@type":"Person","name":"sterliakov"},"datePublished":"2025-02-22T03:57:44Z","url":"https://github.com/python/mypy/issues/18719"}
| 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:6f8bbed8-df12-f395-ece1-39d77b3e7220 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 87DC:1D7BA0:5EC3F4:7F1B43:6B003381 |
| html-safe-nonce | ea05ad4bee149bc26f3b143b960f45bdc80811f039a98dcd6e703f168c643fff |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4N0RDOjFEN0JBMDo1RUMzRjQ6N0YxQjQzOjZCMDAzMzgxIiwidmlzaXRvcl9pZCI6IjMwNjczMzAyMDcwOTk2NjMyMzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | bfbd1ea5487c1afd734a0532d5d35461f7ffdcb50f1004d26743253e5f066a67 |
| hovercard-subject-tag | issue:2870464077 |
| 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/18719/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9e99860e8d3f83ecc89f833d9ea9eef4198f0b5757bd0a3cd289a12f04962935/python/mypy/issues/18719 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9e99860e8d3f83ecc89f833d9ea9eef4198f0b5757bd0a3cd289a12f04962935/python/mypy/issues/18719 |
| og:image:alt | Bug Report typing.Literal is supposed to accept enum members and not non-members (such as methods or explicit nonmember attributes). mypy fails to enforce the latter and support the former. To Repr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sterliakov |
| 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