gh-101541: [Enum] create flag psuedo-member without calling original __new__ (GH-101590)

This commit is contained in:
Ethan Furman 2023-02-05 19:29:06 -08:00 committed by GitHub
parent d3e2dd6e71
commit ef7c2bfcf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 3 deletions

View file

@ -2855,6 +2855,46 @@ class TestSpecial(unittest.TestCase):
[TTuple(id=0, a=0, blist=[]), TTuple(id=1, a=2, blist=[4]), TTuple(id=2, a=4, blist=[0, 1, 2])],
)
def test_flag_with_custom_new(self):
class FlagFromChar(IntFlag):
def __new__(cls, c):
value = 1 << c
self = int.__new__(cls, value)
self._value_ = value
return self
#
a = ord('a')
#
self.assertEqual(FlagFromChar.a, 158456325028528675187087900672)
self.assertEqual(FlagFromChar.a|1, 158456325028528675187087900673)
#
#
class FlagFromChar(Flag):
def __new__(cls, c):
value = 1 << c
self = object.__new__(cls)
self._value_ = value
return self
#
a = ord('a')
z = 1
#
self.assertEqual(FlagFromChar.a.value, 158456325028528675187087900672)
self.assertEqual((FlagFromChar.a|FlagFromChar.z).value, 158456325028528675187087900674)
#
#
class FlagFromChar(int, Flag, boundary=KEEP):
def __new__(cls, c):
value = 1 << c
self = int.__new__(cls, value)
self._value_ = value
return self
#
a = ord('a')
#
self.assertEqual(FlagFromChar.a, 158456325028528675187087900672)
self.assertEqual(FlagFromChar.a|1, 158456325028528675187087900673)
class TestOrder(unittest.TestCase):
"test usage of the `_order_` attribute"