Skip to content

gh-107538: [Enum] fix handling of inverted/negative values #132273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k
# now set the __repr__ for the value
classdict['_value_repr_'] = metacls._find_data_repr_(cls, bases)
#
# Flag structures (will be removed if final class is not a Flag
# Flag structures (will be removed if final class is not a Flag)
classdict['_boundary_'] = (
boundary
or getattr(first_enum, '_boundary_', None)
Expand All @@ -544,6 +544,29 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k
classdict['_singles_mask_'] = 0
classdict['_all_bits_'] = 0
classdict['_inverted_'] = None
# check for negative flag values and invert if found (using _proto_members)
if Flag is not None and bases and issubclass(bases[-1], Flag):
bits = 0
inverted = []
for n in member_names:
p = classdict[n]
if isinstance(p.value, int):
if p.value < 0:
inverted.append(p)
else:
bits |= p.value
elif p.value is None:
pass
elif isinstance(p.value, tuple) and p.value and isinstance(p.value[0], int):
if p.value[0] < 0:
inverted.append(p)
else:
bits |= p.value[0]
for p in inverted:
if isinstance(p.value, int):
p.value = bits & p.value
else:
p.value = (bits & p.value[0], ) + p.value[1:]
try:
classdict['_%s__in_progress' % cls] = True
enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
Expand Down Expand Up @@ -1485,7 +1508,10 @@ def _missing_(cls, value):
)
if value < 0:
neg_value = value
value = all_bits + 1 + value
if cls._boundary_ in (EJECT, KEEP):
value = all_bits + 1 + value
else:
value = singles_mask & value
# get members and unknown
unknown = value & ~flag_mask
aliases = value & ~singles_mask
Expand Down
37 changes: 37 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,12 +1001,18 @@ class OpenAB(self.enum_type):
self.assertIs(~(A|B), OpenAB(252))
self.assertIs(~AB_MASK, OpenAB(0))
self.assertIs(~OpenAB(0), AB_MASK)
self.assertIs(OpenAB(~4), OpenAB(251))
else:
self.assertIs(~A, B)
self.assertIs(~B, A)
self.assertIs(OpenAB(~1), B)
self.assertIs(OpenAB(~2), A)
self.assertIs(~(A|B), OpenAB(0))
self.assertIs(~AB_MASK, OpenAB(0))
self.assertIs(~OpenAB(0), (A|B))
self.assertIs(OpenAB(~3), OpenAB(0))
self.assertIs(OpenAB(~4), OpenAB(3))
self.assertIs(OpenAB(~33), B)
#
class OpenXYZ(self.enum_type):
X = 4
Expand All @@ -1030,13 +1036,38 @@ class OpenXYZ(self.enum_type):
self.assertIs(~X, Y|Z)
self.assertIs(~Y, X|Z)
self.assertIs(~Z, X|Y)
self.assertIs(OpenXYZ(~4), Y|Z)
self.assertIs(OpenXYZ(~2), X|Z)
self.assertIs(OpenXYZ(~1), X|Y)
self.assertIs(~(X|Y), Z)
self.assertIs(~(X|Z), Y)
self.assertIs(~(Y|Z), X)
self.assertIs(~(X|Y|Z), OpenXYZ(0))
self.assertIs(~XYZ_MASK, OpenXYZ(0))
self.assertTrue(~OpenXYZ(0), (X|Y|Z))

def test_assigned_negative_value(self):
class X(self.enum_type):
A = auto()
B = auto()
C = A | B
D = ~A
self.assertEqual(list(X), [X.A, X.B])
self.assertIs(~X.A, X.B)
self.assertIs(X.D, X.B)
self.assertEqual(X.D.value, 2)
#
class Y(self.enum_type):
A = auto()
B = auto()
C = A | B
D = ~A
E = auto()
self.assertEqual(list(Y), [Y.A, Y.B, Y.E])
self.assertIs(~Y.A, Y.B|Y.E)
self.assertIs(Y.D, Y.B|Y.E)
self.assertEqual(Y.D.value, 6)


class TestPlainEnumClass(_EnumTests, _PlainOutputTests, unittest.TestCase):
enum_type = Enum
Expand Down Expand Up @@ -3668,6 +3699,8 @@ class SkipFlag(enum.Flag):
C = 4 | B
#
self.assertTrue(SkipFlag.C in (SkipFlag.A|SkipFlag.C))
self.assertTrue(SkipFlag.B in SkipFlag.C)
self.assertIs(SkipFlag(~1), SkipFlag.B)
self.assertRaisesRegex(ValueError, 'SkipFlag.. invalid value 42', SkipFlag, 42)
#
class SkipIntFlag(enum.IntFlag):
Expand All @@ -3676,6 +3709,8 @@ class SkipIntFlag(enum.IntFlag):
C = 4 | B
#
self.assertTrue(SkipIntFlag.C in (SkipIntFlag.A|SkipIntFlag.C))
self.assertTrue(SkipIntFlag.B in SkipIntFlag.C)
self.assertIs(SkipIntFlag(~1), SkipIntFlag.B|SkipIntFlag.C)
self.assertEqual(SkipIntFlag(42).value, 42)
#
class MethodHint(Flag):
Expand Down Expand Up @@ -4715,6 +4750,8 @@ class Color(Flag):
BLUE = 4
WHITE = -1
# no error means success
self.assertEqual(list(Color.WHITE), [Color.RED, Color.GREEN, Color.BLUE])
self.assertEqual(Color.WHITE.value, 7)


class TestInternals(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix flag mask inversion when unnamed flags exist.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate NEWS entries?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing a different aspect of the same issue.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix :class:`!Flag` inversion when flag set has missing values
(:class:`!IntFlag` still flips all bits); fix negative assigned values
during flag creation (both :class:`!Flag` and :class:`!IntFlag` ignore
missing values).
Loading