8000 gh-109022: [Enum] require `names=()` to create empty enum type by ethanfurman · Pull Request #109048 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-109022: [Enum] require names=() to create empty enum type #109048

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 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
revert ValueError to TypeError; update NEWS
  • Loading branch information
ethanfurman committed Sep 7, 2023
commit 6f3a5ebc4590af3ee192e04339e4bd032c03ae6d
4 changes: 2 additions & 2 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ def __call__(cls, value, names=None, *values, module=None, qualname=None, type=N
# 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 ValueError(
raise TypeError(
f"{cls} has no members; specify `names=()` if you meant to create a new, empty, enum"
)
return cls._create_(
Expand Down Expand Up @@ -1122,7 +1122,7 @@ def __new__(cls, value):
# still not found -- verify that members exist, in-case somebody got here mistakenly
# (such as via super when trying to override __new__)
if not cls._member_map_:
raise ValueError("%r has no members defined" % cls)
raise TypeError("%r has no members defined" % cls)
#
# still not found -- try _missing_ hook
try:
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def spam(cls):

def test_bad_new_super(self):
with self.assertRaisesRegex(
ValueError,
TypeError,
'has no members defined',
):
class BadSuper(self.enum_type):
Expand Down Expand Up @@ -601,7 +601,7 @@ class SubEnum(SuperEnum):
self.assertTrue('description' in dir(SubEnum.sample), dir(SubEnum.sample))

def test_empty_enum_has_no_values(self):
with self.assertRaisesRegex(ValueError, "<.... 'NewBaseEnum'> has no members"):
with self.assertRaisesRegex(TypeError, "<.... 'NewBaseEnum'> has no members"):
self.NewBaseEnum(7)

def test_enum_in_enum_out(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Enum: raise :exc:`ValueError` if ``Enum.__new__`` is called when no members
exist; this could be from an incorrect ``super.__new__`` call, and other
incorrect usage.
Enum: require ``names=()`` or ``type=...`` to create an empty enum using
the functional syntax.
0