Title: Enum-like user classes · Issue #4311 · python/mypy · GitHub
Open Graph Title: Enum-like user classes · Issue #4311 · python/mypy
X Title: Enum-like user classes · Issue #4311 · python/mypy
Description: Feature request: Allow annotating a class as enum-like for mypy type checking purposes. I have classes dealing with a value-like (immutable) type. One of the classes in that family is an enum-like class, whose values are of the new immut...
Open Graph Description: Feature request: Allow annotating a class as enum-like for mypy type checking purposes. I have classes dealing with a value-like (immutable) type. One of the classes in that family is an enum-like ...
X Description: Feature request: Allow annotating a class as enum-like for mypy type checking purposes. I have classes dealing with a value-like (immutable) type. One of the classes in that family is an enum-like ...
Opengraph URL: https://github.com/python/mypy/issues/4311
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Enum-like user classes","articleBody":"Feature request: Allow annotating a class as enum-like for `mypy` type checking purposes.\r\n\r\nI have classes dealing with a value-like (immutable) type.\r\nOne of the classes in that family is an enum-like class, whose values are of the new immutable type.\r\nI tried to implement this by deriving from the standard library `Enum`, but it has pretty strong opinions about the implementation which do not match the way my value type works (it is too different from a plain integer).\r\n\r\nNo worries, I just implemented my own `MyEnumBase` base class (and `MyEnumMeta` for it), loosely inspired by `Enum`.\r\nIt works fine.\r\n\r\nThe problem is convincing `mypy` to type check it.\r\nSpecifically, if I write:\r\n\r\n```python\r\nclass EnumExample(MyEnumBase):\r\n FOO = MyAuto()\r\n```\r\n\r\nThen `mypy` will not recognize that `EnumExample.FOO` has the type `EnumExample`.\r\nI can work around this by the following horrible hack:\r\n\r\n```python\r\nclass IsEnumBase:\r\n \"\"\"Check whether a given class is an enumerated base class.\"\"\"\r\n def __eq__(self, class_name: Any) -> bool:\r\n return class_name == 'enum.Enum' or class_name == 'my.module.MyEnumBase'\r\n\r\nmypy.nodes.ENUM_BASECLASS = IsEnumBase()\r\n```\r\n\r\nWhich mostly works, but, *shudder* (is there a better way to achieve the same effect?)\r\n\r\nIt would be nice if I could annotate `MyEnumBase` somehow to let `mypy` it is an enum base class.\r\nFor example, if the single `mypy.nodes.ENUM_BASECLASS` was replaced by a set of class names, so that it were possible to insert additional class names into it (and provide a command line flag to `mypy` for doing that).","author":{"url":"https://github.com/orenbenkiki","@type":"Person","name":"orenbenkiki"},"datePublished":"2017-12-03T09:54:45Z","url":"https://github.com/python/mypy/issues/4311"}
| 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:2cfcf2df-b15d-c57c-3d44-0c7a528790e6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8338:1DD784:C1B897:10396E4:6B004E73 |
| html-safe-nonce | f4f33a32bf3a68966d8ef172816c2d6bbfa5e50d40e9ae73cd07a9ec4ce36f6e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MzM4OjFERDc4NDpDMUI4OTc6MTAzOTZFNDo2QjAwNEU3MyIsInZpc2l0b3JfaWQiOiI4NTg1OTExMjQ2MTU1MzI2NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a58e595eb08d068bed1099d69ae300e8f2e8933d47a304f2391003800d8f0bb1 |
| hovercard-subject-tag | issue:278759841 |
| 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/4311/issue_layout |
| twitter:image | https://opengraph.githubassets.com/38296f9ae4d9ef1c69061f88205fa9f92a69bd265e21178a325134fd15e34045/python/mypy/issues/4311 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/38296f9ae4d9ef1c69061f88205fa9f92a69bd265e21178a325134fd15e34045/python/mypy/issues/4311 |
| og:image:alt | Feature request: Allow annotating a class as enum-like for mypy type checking purposes. I have classes dealing with a value-like (immutable) type. One of the classes in that family is an enum-like ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | orenbenkiki |
| hostname | github.com |
| expected-hostname | github.com |
| None | f9517bf360d46638a1e5e7daee7158842fe5ef43c9000afa0e127564a7c2f39b |
| 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 | b27c5dde30676028abd5c5fc53ce5c9b625f8281 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width