From cbfcc67170d459bcf3e9d63d2f44eadec740bf69 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Tue, 8 Dec 2020 11:52:58 -0800 Subject: [PATCH] bpo-41907: [Enum] fix format() behavior for IntFlag (GH-22497) (GH-23704) (cherry picked from commit 37440eef7f9a0c27e13fc9ce0850574bb00688b0) --- Lib/enum.py | 2 +- Lib/test/test_enum.py | 14 ++++++++++++++ .../2020-10-02-10-19-49.bpo-41907.wiIEsz.rst | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2020-10-02-10-19-49.bpo-41907.wiIEsz.rst diff --git a/Lib/enum.py b/Lib/enum.py index da809e26891..da0657f66ce 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -663,7 +663,7 @@ class Enum(metaclass=EnumMeta): # the value # 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: cls = str val = str(self) diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 7f6e6bf092b..484a2de6a9a 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -569,12 +569,15 @@ class TestEnum(unittest.TestCase): class Test1Enum(MyMethodEnum, int, MyStrEnum): One = 1 Two = 2 + self.assertTrue(Test1Enum._member_type_ is int) self.assertEqual(str(Test1Enum.One), 'MyStr') + self.assertEqual(format(Test1Enum.One, ''), 'MyStr') # class Test2Enum(MyStrEnum, MyMethodEnum): One = 1 Two = 2 self.assertEqual(str(Test2Enum.One), 'MyStr') + self.assertEqual(format(Test1Enum.One, ''), 'MyStr') def test_inherited_data_type(self): class HexInt(int): @@ -2172,6 +2175,11 @@ class TestFlag(unittest.TestCase): self.assertEqual(repr(~(Open.RO | Open.CE)), '') self.assertEqual(repr(~(Open.WO | Open.CE)), '') + 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): Perm = self.Perm for i in Perm: @@ -2504,6 +2512,7 @@ class TestIntFlag(unittest.TestCase): def test_type(self): Perm = self.Perm + self.assertTrue(Perm._member_type_ is int) Open = self.Open for f in Perm: self.assertTrue(isinstance(f, Perm)) @@ -2583,6 +2592,11 @@ class TestIntFlag(unittest.TestCase): self.assertEqual(repr(~(Open.WO | Open.CE)), '') self.assertEqual(repr(Open(~4)), '') + 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): Perm = self.Perm for i in Perm: diff --git a/Misc/NEWS.d/next/Library/2020-10-02-10-19-49.bpo-41907.wiIEsz.rst b/Misc/NEWS.d/next/Library/2020-10-02-10-19-49.bpo-41907.wiIEsz.rst new file mode 100644 index 00000000000..aa337b38046 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-10-02-10-19-49.bpo-41907.wiIEsz.rst @@ -0,0 +1 @@ +fix `format()` behavior for `IntFlag`