Skip to content

Commit

Permalink
Explicitly format the enum as ClassName.FieldName.
Browse files Browse the repository at this point in the history
Context: Python 3.11 changed the `__str__()` value of `IntEnum`, `IntFlag`, and `StrEnum` to be their _value_'s `__str__()`. e.g. `str(SomeIntEnum.A) == "1"` in Python 3.11+, but `str(SomeIntEnum.A) == "SomeIntEnum.A"` before.

This change makes the formatting consistent.

PiperOrigin-RevId: 546304776
  • Loading branch information
yilei authored and copybara-github committed Jul 7, 2023
1 parent 1b6f054 commit eccd9ff
Show file tree
Hide file tree
Showing 2 changed files with 11 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 @@ -2830,7 +2830,7 @@ def decorator(cls, module=module):
if module is None:
module = cls.__module__
for value in cls:
constant('{}.{}'.format(module, str(value)), value)
constant('{}.{}.{}'.format(module, cls.__name__, value.name), value)
return cls

if cls is None:
Expand Down
13 changes: 10 additions & 3 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2378,19 +2378,26 @@ class SomeEnum(enum.Enum):
A = 0,
B = 1

@config.constants_from_enum(module='enum_module')
class SomeIntEnum(enum.IntEnum):
A = 0
B = 1

@config.configurable
def f(a, b):
return a, b
def f(a, b, c):
return a, b, c

config.parse_config("""
f.a = %enum_module.SomeEnum.A
f.b = %SomeEnum.B
f.c = %SomeIntEnum.A
""")
# pylint: disable=no-value-for-parameter
a, b = f()
a, b, c = f()
# pylint: enable=no-value-for-parameter
self.assertEqual(SomeEnum.A, a)
self.assertEqual(SomeEnum.B, b)
self.assertEqual(SomeIntEnum.A, c)

def testConstantsFromEnumWithModule(self):

Expand Down

0 comments on commit eccd9ff

Please sign in to comment.