Skip to content

Commit

Permalink
Explicitly using __members__ to access the enum values.
Browse files Browse the repository at this point in the history
In Python 3.11, iterating through a Flag enum will only return the primary values without composite values.

PiperOrigin-RevId: 569549010
  • Loading branch information
yilei authored and copybara-github committed Oct 2, 2023
1 parent ddd964c commit dd9c0ff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 11 additions & 3 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down

0 comments on commit dd9c0ff

Please sign in to comment.