Title: Is `Enum`'s `_generate_next_value_` really a `@staticmethod`? · Issue #10428 · python/typeshed · GitHub
Open Graph Title: Is `Enum`'s `_generate_next_value_` really a `@staticmethod`? · Issue #10428 · python/typeshed
X Title: Is `Enum`'s `_generate_next_value_` really a `@staticmethod`? · Issue #10428 · python/typeshed
Description: I was playing around with a custom enum and wanted to give it custom auto values. But come time to override _generate_next_value_ and pyright tells me that: Method "_generate_next_value_" overrides class "Enum" in an incompatible manner....
Open Graph Description: I was playing around with a custom enum and wanted to give it custom auto values. But come time to override _generate_next_value_ and pyright tells me that: Method "_generate_next_value_" overrides...
X Description: I was playing around with a custom enum and wanted to give it custom auto values. But come time to override _generate_next_value_ and pyright tells me that: Method "_generate_next_value_"...
Opengraph URL: https://github.com/python/typeshed/issues/10428
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Is `Enum`'s `_generate_next_value_` really a `@staticmethod`?","articleBody":"I was playing around with a custom enum and wanted to give it custom `auto` values. But come time to override `_generate_next_value_` and pyright tells me that: `Method \"_generate_next_value_\" overrides class \"Enum\" in an incompatible manner. Base method is declared as a staticmethod but override is not`.\r\n\r\nmypy:\r\n```\r\nerror: Signature of \"_generate_next_value_\" incompatible with supertype \"Enum\" [override]\r\nnote: Superclass:\r\nnote: def _generate_next_value_(name: str, start: int, count: int, last_values: list[Any]) -> Any\r\nnote: Subclass:\r\nnote: def _generate_next_value_(name, *_: Any) -> Any\r\n```\r\n\r\nThe following code in Python 3.9 works as expected, although with a type error:\r\n```py\r\nfrom enum import Enum, EnumMeta, auto, unique\r\nfrom typing import cast\r\nfrom typing_extensions import override\r\n\r\nclass CaptureMethodMeta(EnumMeta):\r\n # Allow checking if simple string is enum\r\n @override\r\n def __contains__(self, other: object):\r\n try:\r\n self(other)\r\n except ValueError:\r\n return False\r\n return True\r\n\r\n@unique\r\n# TODO: Replace with StrEnum in Python 3.11\r\nclass CaptureMethodEnum(str, Enum, metaclass=CaptureMethodMeta):\r\n # Allow TOML to save as a simple string\r\n @override\r\n def __repr__(self):\r\n return self.value\r\n __str__ = __repr__\r\n\r\n @override\r\n def _generate_next_value_(name: str, *_):\r\n return name\r\n\r\n NONE = \"\"\r\n BITBLT = auto()\r\n WINDOWS_GRAPHICS_CAPTURE = auto()\r\n PRINTWINDOW_RENDERFULLCONTENT = auto()\r\n DESKTOP_DUPLICATION = auto()\r\n VIDEO_CAPTURE_DEVICE = auto()\r\n\r\nbitblt: str = cast(str, \"BITBLT\")\r\nbar: str = cast(str, \"bar\")\r\nprint(bitblt == CaptureMethodEnum.BITBLT)\r\nprint(bar == CaptureMethodEnum.BITBLT)\r\nprint(\"BITBLT\" in CaptureMethodEnum)\r\nprint(\"bar\" in CaptureMethodEnum)\r\n```\r\n\r\nIf I decorate `_generate_next_value_` with `@staticmethod`, I get the following error:\r\n```\r\nTraceback (most recent call last):\r\n File \"c:\\Users\\Avasam\\Desktop\\from enum import Enum, EnumMeta, unique.py\", line 18, in \u003cmodule>\r\n class CaptureMethodEnum(str, Enum, metaclass=CaptureMethodMeta):\r\n File \"c:\\Users\\Avasam\\Desktop\\from enum import Enum, EnumMeta, unique.py\", line 31, in CaptureMethodEnum\r\n BITBLT = auto()\r\n File \"C:\\Program Files\\Python39\\lib\\enum.py\", line 142, in __setitem__\r\n value.value = self._generate_next_value(\r\nTypeError: 'staticmethod' object is not callable\r\n```\r\n","author":{"url":"https://github.com/Avasam","@type":"Person","name":"Avasam"},"datePublished":"2023-07-10T05:25:00Z","url":"https://github.com/python/typeshed/issues/10428"}
| 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:82c5db03-c15f-e274-3b66-b3c59ee96e51 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A7B8:BACA4:33F523C:4519AD5:6B0044A4 |
| html-safe-nonce | 070f41dd11e6140c9e1dceaf4dcf25ccde8d3b224b7d378443090a661c8c4206 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBN0I4OkJBQ0E0OjMzRjUyM0M6NDUxOUFENTo2QjAwNDRBNCIsInZpc2l0b3JfaWQiOiIyNjU1MjcyNDM2MjkwODMxNTI0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 26408ecb761152c6de01f3da259d9738d9df153930b8a57b484e51b912fd9ca0 |
| hovercard-subject-tag | issue:1795974999 |
| 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/typeshed/10428/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e61e688b2499d78eb6e7f22fbd7845542f84b32afa4f8f6492709299cc59f503/python/typeshed/issues/10428 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e61e688b2499d78eb6e7f22fbd7845542f84b32afa4f8f6492709299cc59f503/python/typeshed/issues/10428 |
| og:image:alt | I was playing around with a custom enum and wanted to give it custom auto values. But come time to override _generate_next_value_ and pyright tells me that: Method "_generate_next_value_" overrides... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Avasam |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| 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