]> granicus.if.org Git - python/commitdiff
bpo-30616: Functional API of enum allows to create empty enums. (#2304)
authorDong-hee Na <donghee.na92@gmail.com>
Wed, 21 Jun 2017 16:52:32 +0000 (01:52 +0900)
committerethanfurman <ethan@stoneleaf.us>
Wed, 21 Jun 2017 16:52:32 +0000 (09:52 -0700)
* bpo-30616: Functional API of enum allows to create empty enums.

* Update NEWS

move addition to avoid conflict

Lib/enum.py
Lib/test/test_enum.py
Misc/NEWS

index 056400d04c94a1d2c98c6880840ecf13bf991b41..73dd613887d22b55d15bf7be18a927db446506b2 100644 (file)
@@ -381,7 +381,7 @@ class EnumMeta(type):
         # special processing needed for names?
         if isinstance(names, str):
             names = names.replace(',', ' ').split()
-        if isinstance(names, (tuple, list)) and isinstance(names[0], str):
+        if isinstance(names, (tuple, list)) and names and isinstance(names[0], str):
             original_names, names = names, []
             last_values = []
             for count, name in enumerate(original_names):
index a97308164403cb3b33b771e238b13618661c590a..ea52de7a5f39c1dcf2075a854aa291ed9c513dc3 100644 (file)
@@ -2291,6 +2291,26 @@ class TestIntFlag(unittest.TestCase):
             self.assertIs(type(e), Perm)
 
 
+    def test_programatic_function_from_empty_list(self):
+        Perm = enum.IntFlag('Perm', [])
+        lst = list(Perm)
+        self.assertEqual(len(lst), len(Perm))
+        self.assertEqual(len(Perm), 0, Perm)
+        Thing = enum.Enum('Thing', [])
+        lst = list(Thing)
+        self.assertEqual(len(lst), len(Thing))
+        self.assertEqual(len(Thing), 0, Thing)
+
+
+    def test_programatic_function_from_empty_tuple(self):
+        Perm = enum.IntFlag('Perm', ())
+        lst = list(Perm)
+        self.assertEqual(len(lst), len(Perm))
+        self.assertEqual(len(Perm), 0, Perm)
+        Thing = enum.Enum('Thing', ())
+        self.assertEqual(len(lst), len(Thing))
+        self.assertEqual(len(Thing), 0, Thing)
+
     def test_containment(self):
         Perm = self.Perm
         R, W, X = Perm
index f8833aafd698827e44c18cdf699f19e6e945b41d..299d9c2784b67bbe7e69be4678825155fe008c1d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -385,6 +385,9 @@ Library
   correctly returns the ``127.0.0.1`` host, instead of treating ``@evil.com``
   as the host in an authentification (``login@host``).
 
+- bpo-30616: Functional API of enum allows to create empty enums.
+  Patched by Dong-hee Na
+
 - bpo-30038: Fix race condition between signal delivery and wakeup file
   descriptor.  Patch by Nathaniel Smith.