[Fix] fix config type inconsistency (#1575)

* [Fix] fix config type inconsistency

* [Fix] Fix unit test
pull/1585/head
Jiazhen Wang 2021-12-14 13:17:57 +08:00 committed by GitHub
parent 250fadc210
commit 88e017337a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 14 deletions

View File

@ -310,16 +310,19 @@ class Config:
if len(b) <= k: if len(b) <= k:
raise KeyError(f'Index {k} exceeds the length of list {b}') raise KeyError(f'Index {k} exceeds the length of list {b}')
b[k] = Config._merge_a_into_b(v, b[k], allow_list_keys) b[k] = Config._merge_a_into_b(v, b[k], allow_list_keys)
elif isinstance(v, elif isinstance(v, dict):
dict) and k in b and not v.pop(DELETE_KEY, False): if k in b and not v.pop(DELETE_KEY, False):
allowed_types = (dict, list) if allow_list_keys else dict allowed_types = (dict, list) if allow_list_keys else dict
if not isinstance(b[k], allowed_types): if not isinstance(b[k], allowed_types):
raise TypeError( raise TypeError(
f'{k}={v} in child config cannot inherit from base ' f'{k}={v} in child config cannot inherit from '
f'because {k} is a dict in the child config but is of ' f'base because {k} is a dict in the child config '
f'type {type(b[k])} in base config. You may set ' f'but is of type {type(b[k])} in base config. '
f'`{DELETE_KEY}=True` to ignore the base config') f'You may set `{DELETE_KEY}=True` to ignore the '
b[k] = Config._merge_a_into_b(v, b[k], allow_list_keys) f'base config.')
b[k] = Config._merge_a_into_b(v, b[k], allow_list_keys)
else:
b[k] = ConfigDict(v)
else: else:
b[k] = v b[k] = v
return b return b

View File

@ -1,2 +1,3 @@
_base_ = './base.py' _base_ = './base.py'
item2 = {'b': 0, '_delete_': True} item1 = {'a': 0, '_delete_': True}
item2 = {'b': 0}

View File

@ -10,7 +10,7 @@ from pathlib import Path
import pytest import pytest
import yaml import yaml
from mmcv import Config, DictAction, dump, load from mmcv import Config, ConfigDict, DictAction, dump, load
data_path = osp.join(osp.dirname(osp.dirname(__file__)), 'data') data_path = osp.join(osp.dirname(osp.dirname(__file__)), 'data')
@ -347,12 +347,16 @@ def test_merge_delete():
cfg_file = osp.join(data_path, 'config/delete.py') cfg_file = osp.join(data_path, 'config/delete.py')
cfg = Config.fromfile(cfg_file) cfg = Config.fromfile(cfg_file)
# cfg.field # cfg.field
assert cfg.item1 == [1, 2] assert cfg.item1 == dict(a=0)
assert cfg.item2 == dict(b=0) assert cfg.item2 == dict(a=0, b=0)
assert cfg.item3 is True assert cfg.item3 is True
assert cfg.item4 == 'test' assert cfg.item4 == 'test'
assert '_delete_' not in cfg.item2 assert '_delete_' not in cfg.item2
# related issue: https://github.com/open-mmlab/mmcv/issues/1570
assert type(cfg.item1) == ConfigDict
assert type(cfg.item2) == ConfigDict
def test_merge_intermediate_variable(): def test_merge_intermediate_variable():