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

(cherry picked from commit 37440eef7f)
This commit is contained in:
Miss Islington (bot) 2020-12-08 11:52:24 -08:00 committed by GitHub
parent a3a4bf3b8d
commit 14eaa7d752
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

@ -657,7 +657,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

@ -570,12 +570,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):
@ -2170,6 +2173,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:
@ -2503,6 +2511,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))
@ -2582,6 +2591,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`