Skip to content

Commit

Permalink
Merge PR from rbgirshick#35
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry-u authored Mar 10, 2021
1 parent 32d5e4a commit 3b0e312
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions yacs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def merge_from_list(self, cfg_list):
d = d[subkey]
subkey = key_list[-1]
_assert_with_logging(subkey in d, "Non-existent key: {}".format(full_key))
value = self._decode_cfg_value(v)
value = self._decode_cfg_value(v, type(d[subkey]))
value = _check_and_coerce_cfg_value_type(value, d[subkey], subkey, full_key)
d[subkey] = value

Expand Down Expand Up @@ -402,7 +402,7 @@ def _load_cfg_py_source(cls, filename):
return cls(module.cfg)

@classmethod
def _decode_cfg_value(cls, value):
def _decode_cfg_value(cls, value, original_type=None):
"""
Decodes a raw config value (e.g., from a yaml config files or command
line argument) into a Python object.
Expand All @@ -418,6 +418,12 @@ def _decode_cfg_value(cls, value):
# All remaining processing is only applied to strings
if not isinstance(value, str):
return value

# if the type of new value is same as the original type, do not literal eval
# this is to avoid conversions such as "+12345" to 12345 (phone number strings)
if original_type is not None and isinstance(value, original_type):
return value

# Try to interpret `value` as a:
# string, number, tuple, list, dict, boolean, or None
try:
Expand Down Expand Up @@ -468,7 +474,8 @@ def _merge_a_into_b(a, b, root, key_list):
full_key = ".".join(key_list + [k])

v = copy.deepcopy(v_)
v = b._decode_cfg_value(v)
original_type = type(b[k]) if k in b else None
v = b._decode_cfg_value(v, original_type)

if k in b:
v = _check_and_coerce_cfg_value_type(v, b[k], k, full_key)
Expand Down Expand Up @@ -519,7 +526,7 @@ def conditional_cast(from_type, to_type):

# Conditionally casts
# list <-> tuple
casts = [(tuple, list), (list, tuple)]
casts = [(tuple, list), (list, tuple), (int, str)]
# For py2: allow converting from str (bytes) to a unicode string
try:
casts.append((str, unicode)) # noqa: F821
Expand Down

0 comments on commit 3b0e312

Please sign in to comment.