gh-93250: [Enum] Change IntEnum boundary to KEEP for backwards compatibility (GH-93302) (GH-93304)

In previous versions of Python if an IntEnum member was combined with another integer type value using a bit-wise operation, the resulting value would still be the IntEnum type.  This change restores that behavior.
(cherry picked from commit 70cfe56caf)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
This commit is contained in:
Miss Islington (bot) 2022-05-27 15:43:13 -07:00 committed by GitHub
parent 3f7abff97b
commit 647426d4fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View file

@ -1575,7 +1575,7 @@ class Flag(Enum, boundary=STRICT):
__rxor__ = __xor__ __rxor__ = __xor__
class IntFlag(int, ReprEnum, Flag, boundary=EJECT): class IntFlag(int, ReprEnum, Flag, boundary=KEEP):
""" """
Support for integer-based Flags Support for integer-based Flags
""" """

View file

@ -3349,7 +3349,10 @@ class OldTestIntFlag(unittest.TestCase):
self.assertIs((Open.WO|Open.CE) & ~Open.WO, Open.CE) self.assertIs((Open.WO|Open.CE) & ~Open.WO, Open.CE)
def test_boundary(self): def test_boundary(self):
self.assertIs(enum.IntFlag._boundary_, EJECT) self.assertIs(enum.IntFlag._boundary_, KEEP)
class Simple(IntFlag, boundary=KEEP):
SINGLE = 1
#
class Iron(IntFlag, boundary=STRICT): class Iron(IntFlag, boundary=STRICT):
ONE = 1 ONE = 1
TWO = 2 TWO = 2
@ -3368,7 +3371,6 @@ class OldTestIntFlag(unittest.TestCase):
EIGHT = 8 EIGHT = 8
self.assertIs(Space._boundary_, EJECT) self.assertIs(Space._boundary_, EJECT)
# #
#
class Bizarre(IntFlag, boundary=KEEP): class Bizarre(IntFlag, boundary=KEEP):
b = 3 b = 3
c = 4 c = 4
@ -3385,6 +3387,12 @@ class OldTestIntFlag(unittest.TestCase):
self.assertEqual(list(Bizarre), [Bizarre.c]) self.assertEqual(list(Bizarre), [Bizarre.c])
self.assertIs(Bizarre(3), Bizarre.b) self.assertIs(Bizarre(3), Bizarre.b)
self.assertIs(Bizarre(6), Bizarre.d) self.assertIs(Bizarre(6), Bizarre.d)
#
simple = Simple.SINGLE | Iron.TWO
self.assertEqual(simple, 3)
self.assertIsInstance(simple, Simple)
self.assertEqual(repr(simple), '<Simple.SINGLE|<Iron.TWO: 2>: 3>')
self.assertEqual(str(simple), '3')
def test_iter(self): def test_iter(self):
Color = self.Color Color = self.Color