mirror of
https://github.com/python/cpython.git
synced 2025-08-30 05:35:08 +00:00
gh-94601: [Enum] fix inheritance for __str__ and friends (GH-94942)
This commit is contained in:
parent
07aeb7405e
commit
c961d14f85
2 changed files with 42 additions and 10 deletions
|
@ -2693,12 +2693,15 @@ class TestSpecial(unittest.TestCase):
|
|||
@dataclass
|
||||
class Foo:
|
||||
__qualname__ = 'Foo'
|
||||
a: int = 0
|
||||
a: int
|
||||
class Entries(Foo, Enum):
|
||||
ENTRY1 = Foo(1)
|
||||
ENTRY1 = 1
|
||||
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: Foo(a=1)>')
|
||||
|
||||
def test_repr_with_non_data_type_mixin(self):
|
||||
def test_repr_with_init_data_type_mixin(self):
|
||||
# non-data_type is a mixin that doesn't define __new__
|
||||
class Foo:
|
||||
def __init__(self, a):
|
||||
|
@ -2706,10 +2709,23 @@ class TestSpecial(unittest.TestCase):
|
|||
def __repr__(self):
|
||||
return f'Foo(a={self.a!r})'
|
||||
class Entries(Foo, Enum):
|
||||
ENTRY1 = Foo(1)
|
||||
|
||||
ENTRY1 = 1
|
||||
#
|
||||
self.assertEqual(repr(Entries.ENTRY1), '<Entries.ENTRY1: Foo(a=1)>')
|
||||
|
||||
def test_repr_and_str_with_non_data_type_mixin(self):
|
||||
# non-data_type is a mixin that doesn't define __new__
|
||||
class Foo:
|
||||
def __repr__(self):
|
||||
return 'Foo'
|
||||
def __str__(self):
|
||||
return 'ooF'
|
||||
class Entries(Foo, Enum):
|
||||
ENTRY1 = 1
|
||||
#
|
||||
self.assertEqual(repr(Entries.ENTRY1), 'Foo')
|
||||
self.assertEqual(str(Entries.ENTRY1), 'ooF')
|
||||
|
||||
def test_value_backup_assign(self):
|
||||
# check that enum will add missing values when custom __new__ does not
|
||||
class Some(Enum):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue