Skip to content

Commit

Permalink
Allow redefining constants when in interactive mode.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 552509815
  • Loading branch information
Gin-Config Team authored and copybara-github committed Jul 31, 2023
1 parent eccd9ff commit ddd964c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2797,7 +2797,7 @@ def constant(name, value):
if not config_parser.MODULE_RE.match(name):
raise ValueError("Invalid constant selector '{}'.".format(name))

if _CONSTANTS.matching_selectors(name):
if not _INTERACTIVE_MODE and _CONSTANTS.matching_selectors(name):
err_str = "Constants matching selector '{}' already exist ({})."
raise ValueError(err_str.format(name, _CONSTANTS.matching_selectors(name)))

Expand Down
14 changes: 14 additions & 0 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,20 @@ def duplicate_fn4(): # pylint: disable=unused-variable
config.parse_config(config_str)
self.assertEqual(ConfigurableClass().kwarg1, 'duplicate_fn3')

config.constant('CONST1', 42)
with self.assertRaisesRegex(ValueError, 'already exist'):
config.constant('CONST1', 42)

with config.interactive_mode():
config.constant('CONST1', 43)

config_str = """
configurable2.non_kwarg = %CONST1
"""
config.parse_config(config_str)
non_kwarg, _ = configurable2() # pylint:disable=no-value-for-parameter
self.assertIs(non_kwarg, 43)

def testFinalizeLocksConfig(self):
config.finalize()
with self.assertRaises(RuntimeError):
Expand Down

0 comments on commit ddd964c

Please sign in to comment.