Skip to content

Commit 9237a88

Browse files
committed
bpo-30616: Functional API of enum allows to create empty enums.
1 parent 5ea4c06 commit 9237a88

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

Lib/enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ def _create_(cls, class_name, names=None, *, module=None, qualname=None, type=No
381381
# special processing needed for names?
382382
if isinstance(names, str):
383383
names = names.replace(',', ' ').split()
384-
if isinstance(names, (tuple, list)) and isinstance(names[0], str):
384+
if isinstance(names, (tuple, list)) and names and isinstance(names[0], str):
385385
original_names, names = names, []
386386
last_values = []
387387
for count, name in enumerate(original_names):

Lib/test/test_enum.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,26 @@ def test_programatic_function_from_dict(self):
22912291
self.assertIs(type(e), Perm)
22922292

22932293

2294+
def test_programatic_function_from_empty_list(self):
2295+
Perm = enum.IntFlag('Perm', [])
2296+
lst = list(Perm)
2297+
self.assertEqual(len(lst), len(Perm))
2298+
self.assertEqual(len(Perm), 0, Perm)
2299+
Thing = enum.Enum('Thing', [])
2300+
lst = list(Thing)
2301+
self.assertEqual(len(lst), len(Thing))
2302+
self.assertEqual(len(Thing), 0, Thing)
2303+
2304+
2305+
def test_programatic_function_from_empty_tuple(self):
2306+
Perm = enum.IntFlag('Perm', ())
2307+
lst = list(Perm)
2308+
self.assertEqual(len(lst), len(Perm))
2309+
self.assertEqual(len(Perm), 0, Perm)
2310+
Thing = enum.Enum('Thing', ())
2311+
self.assertEqual(len(lst), len(Thing))
2312+
self.assertEqual(len(Thing), 0, Thing)
2313+
22942314
def test_containment(self):
22952315
Perm = self.Perm
22962316
R, W, X = Perm

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ Extension Modules
368368
Library
369369
-------
370370

371+
- bpo-30616: Functional API of enum allows to create empty enums.
372+
Patched by Dong-hee Na
373+
371374
- bpo-29755: Fixed the lgettext() family of functions in the gettext module.
372375
They now always return bytes.
373376

0 commit comments

Comments
 (0)