]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-103056: [Enum] ensure final _generate_next_value_ is a staticmethod (GH-103062)
authorEthan Furman <ethan@stoneleaf.us>
Mon, 27 Mar 2023 23:25:19 +0000 (16:25 -0700)
committerGitHub <noreply@github.com>
Mon, 27 Mar 2023 23:25:19 +0000 (16:25 -0700)
Lib/enum.py
Lib/test/test_enum.py
Misc/NEWS.d/next/Library/2023-03-27-15-01-16.gh-issue-103056.-Efh5Q.rst [new file with mode: 0644]

index ba927662a43b13d13653be98f80b35b51f6c1ccf..2624a084dc63002d8c740d27fec5f7e229b85b2c 100644 (file)
@@ -518,8 +518,13 @@ class EnumType(type):
         #
         # adjust the sunders
         _order_ = classdict.pop('_order_', None)
+        _gnv = classdict.get('_generate_next_value_')
+        if _gnv is not None and type(_gnv) is not staticmethod:
+            _gnv = staticmethod(_gnv)
         # convert to normal dict
         classdict = dict(classdict.items())
+        if _gnv is not None:
+            classdict['_generate_next_value_'] = _gnv
         #
         # data type of member and the controlling Enum class
         member_type, first_enum = metacls._get_mixins_(cls, bases)
index bb163c46481a42070420ca255bad1f6800c6c885..58c80e7d228b5dda529bdfdc9df04dc3bcecfc02 100644 (file)
@@ -270,6 +270,17 @@ class _EnumTests:
             first = auto()
         self.NewSubEnum = NewSubEnum
         #
+        class LazyGNV(self.enum_type):
+            def _generate_next_value_(name, start, last, values):
+                pass
+        self.LazyGNV = LazyGNV
+        #
+        class BusyGNV(self.enum_type):
+            @staticmethod
+            def _generate_next_value_(name, start, last, values):
+                pass
+        self.BusyGNV = BusyGNV
+        #
         self.is_flag = False
         self.names = ['first', 'second', 'third']
         if issubclass(MainEnum, StrEnum):
@@ -466,6 +477,12 @@ class _EnumTests:
         Main = self.MainEnum
         self.assertIs(Main(Main.first), Main.first)
 
+    def test_gnv_is_static(self):
+        lazy = self.LazyGNV
+        busy = self.BusyGNV
+        self.assertTrue(type(lazy.__dict__['_generate_next_value_']) is staticmethod)
+        self.assertTrue(type(busy.__dict__['_generate_next_value_']) is staticmethod)
+
     def test_hash(self):
         MainEnum = self.MainEnum
         mapping = {}
diff --git a/Misc/NEWS.d/next/Library/2023-03-27-15-01-16.gh-issue-103056.-Efh5Q.rst b/Misc/NEWS.d/next/Library/2023-03-27-15-01-16.gh-issue-103056.-Efh5Q.rst
new file mode 100644 (file)
index 0000000..c892d83
--- /dev/null
@@ -0,0 +1 @@
+Ensure final ``_generate_next_value_`` is a ``staticmethod``.