Mailing List Archive

gh-103056: [Enum] ensure final _generate_next_value_ is a staticmethod (GH-103062)
https://github.com/python/cpython/commit/b838d80085b0162cc2ae7b4db5d2a9d9c6a28366
commit: b838d80085b0162cc2ae7b4db5d2a9d9c6a28366
branch: main
author: Ethan Furman <ethan@stoneleaf.us>
committer: ethanfurman <ethan@stoneleaf.us>
date: 2023-03-27T16:25:19-07:00
summary:

gh-103056: [Enum] ensure final _generate_next_value_ is a staticmethod (GH-103062)

files:
A Misc/NEWS.d/next/Library/2023-03-27-15-01-16.gh-issue-103056.-Efh5Q.rst
M Lib/enum.py
M Lib/test/test_enum.py

diff --git a/Lib/enum.py b/Lib/enum.py
index ba927662a43b..2624a084dc63 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -518,8 +518,13 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k
#
# 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)
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index bb163c46481a..58c80e7d228b 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -270,6 +270,17 @@ class NewSubEnum(NewBaseEnum):
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 @@ def test_enum_in_enum_out(self):
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
index 000000000000..c892d8376503
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-03-27-15-01-16.gh-issue-103056.-Efh5Q.rst
@@ -0,0 +1 @@
+Ensure final ``_generate_next_value_`` is a ``staticmethod``.

_______________________________________________
Python-checkins mailing list
Python-checkins@python.org
https://mail.python.org/mailman/listinfo/python-checkins