Skip to content

Commit b838d80

Browse files
authored
gh-103056: [Enum] ensure final _generate_next_value_ is a staticmethod (GH-103062)
1 parent 56d055a commit b838d80

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Lib/enum.py

+5
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,13 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k
518518
#
519519
# adjust the sunders
520520
_order_ = classdict.pop('_order_', None)
521+
_gnv = classdict.get('_generate_next_value_')
522+
if _gnv is not None and type(_gnv) is not staticmethod:
523+
_gnv = staticmethod(_gnv)
521524
# convert to normal dict
522525
classdict = dict(classdict.items())
526+
if _gnv is not None:
527+
classdict['_generate_next_value_'] = _gnv
523528
#
524529
# data type of member and the controlling Enum class
525530
member_type, first_enum = metacls._get_mixins_(cls, bases)

Lib/test/test_enum.py

+17
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,17 @@ class NewSubEnum(NewBaseEnum):
270270
first = auto()
271271
self.NewSubEnum = NewSubEnum
272272
#
273+
class LazyGNV(self.enum_type):
274+
def _generate_next_value_(name, start, last, values):
275+
pass
276+
self.LazyGNV = LazyGNV
277+
#
278+
class BusyGNV(self.enum_type):
279+
@staticmethod
280+
def _generate_next_value_(name, start, last, values):
281+
pass
282+
self.BusyGNV = BusyGNV
283+
#
273284
self.is_flag = False
274285
self.names = ['first', 'second', 'third']
275286
if issubclass(MainEnum, StrEnum):
@@ -466,6 +477,12 @@ def test_enum_in_enum_out(self):
466477
Main = self.MainEnum
467478
self.assertIs(Main(Main.first), Main.first)
468479

480+
def test_gnv_is_static(self):
481+
lazy = self.LazyGNV
482+
busy = self.BusyGNV
483+
self.assertTrue(type(lazy.__dict__['_generate_next_value_']) is staticmethod)
484+
self.assertTrue(type(busy.__dict__['_generate_next_value_']) is staticmethod)
485+
469486
def test_hash(self):
470487
MainEnum = self.MainEnum
471488
mapping = {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure final ``_generate_next_value_`` is a ``staticmethod``.

0 commit comments

Comments
 (0)