Skip to content

gh-103479: [Enum] require __new__ to be considered a data type #103495

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 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions Doc/howto/enum.rst
Original file line number Diff line number Diff line change
Expand Up @@ -865,17 +865,19 @@ Some rules:
4. When another data type is mixed in, the :attr:`value` attribute is *not the
same* as the enum member itself, although it is equivalent and will compare
equal.
5. %-style formatting: ``%s`` and ``%r`` call the :class:`Enum` class's
5. A ``data type`` is a mixin that defines :meth:`__new__`, or a
:class:`~dataclasses.dataclass`
6. %-style formatting: ``%s`` and ``%r`` call the :class:`Enum` class's
:meth:`__str__` and :meth:`__repr__` respectively; other codes (such as
``%i`` or ``%h`` for IntEnum) treat the enum member as its mixed-in type.
6. :ref:`Formatted string literals <f-strings>`, :meth:`str.format`,
7. :ref:`Formatted string literals <f-strings>`, :meth:`str.format`,
and :func:`format` will use the enum's :meth:`__str__` method.

.. note::

Because :class:`IntEnum`, :class:`IntFlag`, and :class:`StrEnum` are
designed to be drop-in replacements for existing constants, their
:meth:`__str__` method has been reset to their data types
:meth:`__str__` method has been reset to their data types'
:meth:`__str__` method.

When to use :meth:`__new__` vs. :meth:`__init__`
Expand Down
3 changes: 2 additions & 1 deletion Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,7 @@ def _find_data_repr_(mcls, class_name, bases):

@classmethod
def _find_data_type_(mcls, class_name, bases):
# a datatype has a __new__ method, or a __dataclass_fields__ attribute
data_types = set()
base_chain = set()
for chain in bases:
Expand All @@ -986,7 +987,7 @@ def _find_data_type_(mcls, class_name, bases):
if base._member_type_ is not object:
data_types.add(base._member_type_)
break
elif '__new__' in base.__dict__ or '__init__' in base.__dict__:
elif '__new__' in base.__dict__ or '__dataclass_fields__' in base.__dict__:
data_types.add(candidate or base)
break
else:
Expand Down
13 changes: 7 additions & 6 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2737,10 +2737,10 @@ def __repr__(self):
return 'ha hah!'
class Entries(Foo, Enum):
ENTRY1 = 1
self.assertEqual(repr(Entries.ENTRY1), '<Entries.ENTRY1: ha hah!>')
self.assertTrue(Entries.ENTRY1.value == Foo(1), Entries.ENTRY1.value)
self.assertTrue(isinstance(Entries.ENTRY1, Foo))
self.assertTrue(Entries._member_type_ is Foo, Entries._member_type_)
self.assertTrue(Entries.ENTRY1.value == Foo(1), Entries.ENTRY1.value)
self.assertEqual(repr(Entries.ENTRY1), '<Entries.ENTRY1: ha hah!>')
#
# check auto-generated dataclass __repr__ is not used
#
Expand Down Expand Up @@ -2787,8 +2787,7 @@ class Creature(CreatureDataMixin, Enum):
DOG = ('medium', 4)
self.assertRegex(repr(Creature.DOG), "<Creature.DOG: .*CreatureDataMixin object at .*>")

def test_repr_with_init_data_type_mixin(self):
# non-data_type is a mixin that doesn't define __new__
def test_repr_with_init_mixin(self):
class Foo:
def __init__(self, a):
self.a = a
Expand All @@ -2797,9 +2796,9 @@ def __repr__(self):
class Entries(Foo, Enum):
ENTRY1 = 1
#
self.assertEqual(repr(Entries.ENTRY1), '<Entries.ENTRY1: Foo(a=1)>')
self.assertEqual(repr(Entries.ENTRY1), 'Foo(a=1)')

def test_repr_and_str_with_non_data_type_mixin(self):
def test_repr_and_str_with_no_init_mixin(self):
# non-data_type is a mixin that doesn't define __new__
class Foo:
def __repr__(self):
Expand Down Expand Up @@ -2905,6 +2904,8 @@ def __new__(cls, c):

def test_init_exception(self):
class Base:
def __new__(cls, *args):
return object.__new__(cls)
def __init__(self, x):
raise ValueError("I don't like", x)
with self.assertRaises(TypeError):
Expand Down