Skip to content

Commit c271344

Browse files
ronshapiroThe ml_collections Authors
authored andcommitted
Support accept_new_attributes in DEFINE_config_dict.
Also fix a typo. PiperOrigin-RevId: 726444585
1 parent da3b26a commit c271344

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

ml_collections/config_flags/config_flags.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def get_config(config_string):
235235
.lock() method on its instance (if it exists). (default: True)
236236
accept_new_attributes: If `True`, accept to pass arbitrary attributes that
237237
are not originally defined in the `get_config()` dict.
238-
`accept_new_attributes` require `lock_config=False`
238+
`accept_new_attributes` requires `lock_config=False`.
239239
sys_argv: If set, interprets this as the full list of args used in parsing.
240240
This is used to identify which overrides to define as flags. If not
241241
specified, uses the system sys.argv to figure it out.
@@ -268,6 +268,7 @@ def DEFINE_config_dict( # pylint: disable=g-bad-name
268268
help_string: str = 'ConfigDict instance.',
269269
flag_values: flags.FlagValues = FLAGS,
270270
lock_config: bool = True,
271+
accept_new_attributes: bool = False,
271272
sys_argv: Optional[List[str]] = None,
272273
**kwargs) -> flags.FlagHolder:
273274
"""Defines flag for inline `ConfigDict's` compatible with absl flags.
@@ -319,6 +320,9 @@ def DEFINE_config_dict( # pylint: disable=g-bad-name
319320
(default: absl.flags.FLAGS)
320321
lock_config: If set to True, loaded config will be locked through calling
321322
.lock() method on its instance (if it exists). (default: True)
323+
accept_new_attributes: If `True`, accept to pass arbitrary attributes that
324+
are not originally defined in the `config` argument.
325+
`accept_new_attributes` requires `lock_config=False`.
322326
sys_argv: If set, interprets this as the full list of args used in parsing.
323327
This is used to identify which overrides to define as flags. If not
324328
specified, uses the system sys.argv to figure it out.
@@ -329,6 +333,8 @@ def DEFINE_config_dict( # pylint: disable=g-bad-name
329333
"""
330334
if not isinstance(config, config_dict.ConfigDict):
331335
raise TypeError('config should be a ConfigDict')
336+
if accept_new_attributes and lock_config:
337+
raise ValueError('`accept_new_attributes=True` requires lock_config=False')
332338
parser = _InlineConfigParser(name=name, lock_config=lock_config)
333339
flag = _ConfigFlag(
334340
parser=parser,
@@ -337,6 +343,7 @@ def DEFINE_config_dict( # pylint: disable=g-bad-name
337343
default=config,
338344
help_string=help_string,
339345
flag_values=flag_values,
346+
accept_new_attributes=accept_new_attributes,
340347
sys_argv=sys_argv,
341348
**kwargs)
342349

0 commit comments

Comments
 (0)