From dd9c0ff12f5e03bc8bb09d27800fbef5d408c97e Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Fri, 29 Sep 2023 11:47:35 -0700 Subject: [PATCH] Explicitly using `__members__` to access the enum values. In Python 3.11, iterating through a Flag enum will only return the primary values without composite values. PiperOrigin-RevId: 569549010 --- gin/config.py | 2 +- tests/config_test.py | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/gin/config.py b/gin/config.py index 836db58..11cc22c 100644 --- a/gin/config.py +++ b/gin/config.py @@ -2829,7 +2829,7 @@ def decorator(cls, module=module): if module is None: module = cls.__module__ - for value in cls: + for value in cls.__members__.values(): constant('{}.{}.{}'.format(module, cls.__name__, value.name), value) return cls diff --git a/tests/config_test.py b/tests/config_test.py index 44c8c3e..cd7b19c 100644 --- a/tests/config_test.py +++ b/tests/config_test.py @@ -2397,21 +2397,29 @@ class SomeIntEnum(enum.IntEnum): A = 0 B = 1 + @config.constants_from_enum(module='enum_module') + class SomeFlagEnum(enum.Flag): + A = 1 + B = 2 + C = A | B + @config.configurable - def f(a, b, c): - return a, b, c + def f(a, b, c, d): + return a, b, c, d config.parse_config(""" f.a = %enum_module.SomeEnum.A f.b = %SomeEnum.B f.c = %SomeIntEnum.A + f.d = %SomeFlagEnum.C """) # pylint: disable=no-value-for-parameter - a, b, c = f() + a, b, c, d = f() # pylint: enable=no-value-for-parameter self.assertEqual(SomeEnum.A, a) self.assertEqual(SomeEnum.B, b) self.assertEqual(SomeIntEnum.A, c) + self.assertEqual(SomeFlagEnum.C, d) def testConstantsFromEnumWithModule(self):