summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEthan Furman <ethan@stoneleaf.us>2023-09-08 01:19:03 (GMT)
committerGitHub <noreply@github.com>2023-09-08 01:19:03 (GMT)
commitc74e440168fab9bf91346471087a394af13fa2db (patch)
treee9afe2d0f55fff41ed232a524b6ef76694dc76b2
parentb9831e5c98de280870b6d932033b868ef56fa2fa (diff)
downloadcpython-c74e440168fab9bf91346471087a394af13fa2db.zip
cpython-c74e440168fab9bf91346471087a394af13fa2db.tar.gz
cpython-c74e440168fab9bf91346471087a394af13fa2db.tar.bz2
gh-109022: [Enum] require `names=()` to create empty enum type (GH-109048)
add guard so that ``Enum('bar')`` raises a TypeError instead of creating a new enum class called `bar`. To create the new but empty class, use: huh = Enum('bar', names=())
-rw-r--r--Lib/enum.py5
-rw-r--r--Lib/test/test_enum.py11
-rw-r--r--Misc/NEWS.d/next/Library/2023-09-06-19-33-41.gh-issue-108682.35Xnc5.rst2
3 files changed, 14 insertions, 4 deletions
diff --git a/Lib/enum.py b/Lib/enum.py
index 4b99e7b..994a7b9 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -730,6 +730,11 @@ class EnumType(type):
value = (value, names) + values
return cls.__new__(cls, value)
# otherwise, functional API: we're creating a new Enum type
+ if names is None and type is None:
+ # no body? no data-type? possibly wrong usage
+ raise TypeError(
+ f"{cls} has no members; specify `names=()` if you meant to create a new, empty, enum"
+ )
return cls._create_(
class_name=value,
names=names,
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index a838b93..8c1f285 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -316,6 +316,7 @@ class _EnumTests:
return self.name.title()
def __format__(self, spec):
return ''.join(reversed(self.name))
+ self.NewBaseEnum = NewBaseEnum
class NewSubEnum(NewBaseEnum):
first = auto()
self.NewSubEnum = NewSubEnum
@@ -382,10 +383,8 @@ class _EnumTests:
return self.name.title()
def __format__(self, spec):
return ''.join(reversed(self.name))
- NewBaseEnum = self.enum_type('NewBaseEnum', dict(__format__=__format__, __str__=__str__))
- class NewSubEnum(NewBaseEnum):
- first = auto()
- self.NewSubEnum = NewBaseEnum('NewSubEnum', 'first')
+ self.NewBaseEnum = self.enum_type('NewBaseEnum', dict(__format__=__format__, __str__=__str__))
+ self.NewSubEnum = self.NewBaseEnum('NewSubEnum', 'first')
#
def _generate_next_value_(name, start, last, values):
pass
@@ -601,6 +600,10 @@ class _EnumTests:
self.assertTrue('description' not in dir(SubEnum))
self.assertTrue('description' in dir(SubEnum.sample), dir(SubEnum.sample))
+ def test_empty_enum_has_no_values(self):
+ with self.assertRaisesRegex(TypeError, "<.... 'NewBaseEnum'> has no members"):
+ self.NewBaseEnum(7)
+
def test_enum_in_enum_out(self):
Main = self.MainEnum
self.assertIs(Main(Main.first), Main.first)
diff --git a/Misc/NEWS.d/next/Library/2023-09-06-19-33-41.gh-issue-108682.35Xnc5.rst b/Misc/NEWS.d/next/Library/2023-09-06-19-33-41.gh-issue-108682.35Xnc5.rst
new file mode 100644
index 0000000..8c13d43
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-09-06-19-33-41.gh-issue-108682.35Xnc5.rst
@@ -0,0 +1,2 @@
+Enum: require ``names=()`` or ``type=...`` to create an empty enum using
+the functional syntax.