bpo-41907: [Enum] fix format() behavior for IntFlag (GH-22497)

This commit is contained in:
Ethan Furman 2020-12-08 11:14:10 -08:00 committed by GitHub
parent c168b5078f
commit 37440eef7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

@ -658,7 +658,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)

View File

@ -585,12 +585,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):
@ -2251,6 +2254,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:
@ -2590,6 +2598,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))
@ -2669,6 +2678,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:

View File

@ -0,0 +1 @@
fix `format()` behavior for `IntFlag`