Skip to content

gh-103056: [Enum] ensure final _generate_next_value_ is a staticmethod #103062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure final ``_generate_next_value_`` is a ``staticmethod``.