Skip to content

Commit 647426d

Browse files
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 70cfe56) Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
1 parent 3f7abff commit 647426d

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

Lib/enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,7 @@ def __invert__(self):
15751575
__rxor__ = __xor__
15761576

15771577

1578-
class IntFlag(int, ReprEnum, Flag, boundary=EJECT):
1578+
class IntFlag(int, ReprEnum, Flag, boundary=KEEP):
15791579
"""
15801580
Support for integer-based Flags
15811581
"""

Lib/test/test_enum.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3349,7 +3349,10 @@ def test_invert(self):
33493349
self.assertIs((Open.WO|Open.CE) & ~Open.WO, Open.CE)
33503350

33513351
def test_boundary(self):
3352-
self.assertIs(enum.IntFlag._boundary_, EJECT)
3352+
self.assertIs(enum.IntFlag._boundary_, KEEP)
3353+
class Simple(IntFlag, boundary=KEEP):
3354+
SINGLE = 1
3355+
#
33533356
class Iron(IntFlag, boundary=STRICT):
33543357
ONE = 1
33553358
TWO = 2
@@ -3368,7 +3371,6 @@ class Space(IntFlag, boundary=EJECT):
33683371
EIGHT = 8
33693372
self.assertIs(Space._boundary_, EJECT)
33703373
#
3371-
#
33723374
class Bizarre(IntFlag, boundary=KEEP):
33733375
b = 3
33743376
c = 4
@@ -3385,6 +3387,12 @@ class Bizarre(IntFlag, boundary=KEEP):
33853387
self.assertEqual(list(Bizarre), [Bizarre.c])
33863388
self.assertIs(Bizarre(3), Bizarre.b)
33873389
self.assertIs(Bizarre(6), Bizarre.d)
3390+
#
3391+
simple = Simple.SINGLE | Iron.TWO
3392+
self.assertEqual(simple, 3)
3393+
self.assertIsInstance(simple, Simple)
3394+
self.assertEqual(repr(simple), '<Simple.SINGLE|<Iron.TWO: 2>: 3>')
3395+
self.assertEqual(str(simple), '3')
33883396

33893397
def test_iter(self):
33903398
Color = self.Color

0 commit comments

Comments
 (0)