Skip to content

Commit dcc8ce4

Browse files
corona10ethanfurman
authored andcommitted
bpo-30616: Functional API of enum allows to create empty enums. (#2304)
* bpo-30616: Functional API of enum allows to create empty enums. * Update NEWS move addition to avoid conflict
1 parent 5ff7132 commit dcc8ce4

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

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
@@ -385,6 +385,9 @@ Library
385385
correctly returns the ``127.0.0.1`` host, instead of treating ``@evil.com``
386386
as the host in an authentification (``login@host``).
387387

388+
- bpo-30616: Functional API of enum allows to create empty enums.
389+
Patched by Dong-hee Na
390+
388391
- bpo-30038: Fix race condition between signal delivery and wakeup file
389392
descriptor. Patch by Nathaniel Smith.
390393

0 commit comments

Comments
 (0)