bpo-41907: [Enum] fix format() behavior for IntFlag (GH-22497) (GH-23704)
(cherry picked from commit 37440eef7f
)
This commit is contained in:
parent
b947b305a6
commit
cbfcc67170
|
@ -663,7 +663,7 @@ class Enum(metaclass=EnumMeta):
|
||||||
# the value
|
# the value
|
||||||
|
|
||||||
# pure Enum branch, or branch with __str__ explicitly overridden
|
# pure Enum branch, or branch with __str__ explicitly overridden
|
||||||
str_overridden = type(self).__str__ != Enum.__str__
|
str_overridden = type(self).__str__ not in (Enum.__str__, Flag.__str__)
|
||||||
if self._member_type_ is object or str_overridden:
|
if self._member_type_ is object or str_overridden:
|
||||||
cls = str
|
cls = str
|
||||||
val = str(self)
|
val = str(self)
|
||||||
|
|
|
@ -569,12 +569,15 @@ class TestEnum(unittest.TestCase):
|
||||||
class Test1Enum(MyMethodEnum, int, MyStrEnum):
|
class Test1Enum(MyMethodEnum, int, MyStrEnum):
|
||||||
One = 1
|
One = 1
|
||||||
Two = 2
|
Two = 2
|
||||||
|
self.assertTrue(Test1Enum._member_type_ is int)
|
||||||
self.assertEqual(str(Test1Enum.One), 'MyStr')
|
self.assertEqual(str(Test1Enum.One), 'MyStr')
|
||||||
|
self.assertEqual(format(Test1Enum.One, ''), 'MyStr')
|
||||||
#
|
#
|
||||||
class Test2Enum(MyStrEnum, MyMethodEnum):
|
class Test2Enum(MyStrEnum, MyMethodEnum):
|
||||||
One = 1
|
One = 1
|
||||||
Two = 2
|
Two = 2
|
||||||
self.assertEqual(str(Test2Enum.One), 'MyStr')
|
self.assertEqual(str(Test2Enum.One), 'MyStr')
|
||||||
|
self.assertEqual(format(Test1Enum.One, ''), 'MyStr')
|
||||||
|
|
||||||
def test_inherited_data_type(self):
|
def test_inherited_data_type(self):
|
||||||
class HexInt(int):
|
class HexInt(int):
|
||||||
|
@ -2172,6 +2175,11 @@ class TestFlag(unittest.TestCase):
|
||||||
self.assertEqual(repr(~(Open.RO | Open.CE)), '<Open.AC: 3>')
|
self.assertEqual(repr(~(Open.RO | Open.CE)), '<Open.AC: 3>')
|
||||||
self.assertEqual(repr(~(Open.WO | Open.CE)), '<Open.RW: 2>')
|
self.assertEqual(repr(~(Open.WO | Open.CE)), '<Open.RW: 2>')
|
||||||
|
|
||||||
|
def test_format(self):
|
||||||
|
Perm = self.Perm
|
||||||
|
self.assertEqual(format(Perm.R, ''), 'Perm.R')
|
||||||
|
self.assertEqual(format(Perm.R | Perm.X, ''), 'Perm.R|X')
|
||||||
|
|
||||||
def test_or(self):
|
def test_or(self):
|
||||||
Perm = self.Perm
|
Perm = self.Perm
|
||||||
for i in Perm:
|
for i in Perm:
|
||||||
|
@ -2504,6 +2512,7 @@ class TestIntFlag(unittest.TestCase):
|
||||||
|
|
||||||
def test_type(self):
|
def test_type(self):
|
||||||
Perm = self.Perm
|
Perm = self.Perm
|
||||||
|
self.assertTrue(Perm._member_type_ is int)
|
||||||
Open = self.Open
|
Open = self.Open
|
||||||
for f in Perm:
|
for f in Perm:
|
||||||
self.assertTrue(isinstance(f, Perm))
|
self.assertTrue(isinstance(f, Perm))
|
||||||
|
@ -2583,6 +2592,11 @@ class TestIntFlag(unittest.TestCase):
|
||||||
self.assertEqual(repr(~(Open.WO | Open.CE)), '<Open.RW: -524290>')
|
self.assertEqual(repr(~(Open.WO | Open.CE)), '<Open.RW: -524290>')
|
||||||
self.assertEqual(repr(Open(~4)), '<Open.CE|AC|RW|WO: -5>')
|
self.assertEqual(repr(Open(~4)), '<Open.CE|AC|RW|WO: -5>')
|
||||||
|
|
||||||
|
def test_format(self):
|
||||||
|
Perm = self.Perm
|
||||||
|
self.assertEqual(format(Perm.R, ''), '4')
|
||||||
|
self.assertEqual(format(Perm.R | Perm.X, ''), '5')
|
||||||
|
|
||||||
def test_or(self):
|
def test_or(self):
|
||||||
Perm = self.Perm
|
Perm = self.Perm
|
||||||
for i in Perm:
|
for i in Perm:
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
fix `format()` behavior for `IntFlag`
|
Loading…
Reference in New Issue