From 6142c141b5f088b3faa476bd363afaf2d7dd7c4c Mon Sep 17 00:00:00 2001 From: lizz Date: Mon, 7 Jun 2021 11:56:02 +0800 Subject: [PATCH 01/10] Support variables in base files for configs Signed-off-by: lizz --- mmcv/utils/config.py | 54 +++++++++++++++++++++++++++++++++ tests/data/config/t.py | 6 ++++ tests/test_utils/test_config.py | 18 +++++++++++ 3 files changed, 78 insertions(+) create mode 100644 tests/data/config/t.py diff --git a/mmcv/utils/config.py b/mmcv/utils/config.py index f48778de97..893b83a1f9 100644 --- a/mmcv/utils/config.py +++ b/mmcv/utils/config.py @@ -1,10 +1,12 @@ # Copyright (c) Open-MMLab. All rights reserved. import ast +import copy import os.path as osp import platform import shutil import sys import tempfile +import uuid import warnings from argparse import Action, ArgumentParser from collections import abc @@ -120,6 +122,51 @@ def _substitute_predefined_vars(filename, temp_config_name): with open(temp_config_name, 'w') as tmp_config_file: tmp_config_file.write(config_file) + @staticmethod + def _pre_substitute_base_vars(filename, temp_config_name): + """Substitute base variable placehoders to string, so that parsing + would work.""" + with open(filename, 'r', encoding='utf-8') as f: + # Setting encoding explicitly to resolve coding issue on windows + config_file = f.read() + base_var_dict = {} + regexp = r'\{\{\s*base\.([\w\.]+)\s*\}\}' + base_vars = set(re.findall(regexp, config_file)) + for base_var in base_vars: + randstr = f'_{base_var}_{uuid.uuid4().hex.lower()[:6]}' + base_var_dict[randstr] = base_var + regexp = r'\{\{\s*base\.' + base_var + r'\s*\}\}' + config_file = re.sub(regexp, f'"{randstr}"', config_file) + with open(temp_config_name, 'w') as tmp_config_file: + tmp_config_file.write(config_file) + return base_var_dict + + @staticmethod + def _substitute_base_vars(cfg, base_var_dict, base_cfg): + cfg = copy.deepcopy(cfg) + + if isinstance(cfg, dict): + for k, v in cfg.items(): + if isinstance(v, str) and v in base_var_dict: + new_v = base_cfg + for new_k in base_var_dict[v].split('.'): + new_v = new_v[new_k] + cfg[k] = new_v + elif isinstance(v, (list, tuple, dict)): + cfg[k] = Config._substitute_base_vars( + v, base_var_dict, base_cfg) + elif isinstance(cfg, tuple): + cfg = tuple( + Config._substitute_base_vars(c, base_var_dict, base_cfg) + for c in cfg) + elif isinstance(cfg, list): + cfg = [ + Config._substitute_base_vars(c, base_var_dict, base_cfg) + for c in cfg + ] + + return cfg + @staticmethod def _file2dict(filename, use_predefined_variables=True): filename = osp.abspath(osp.expanduser(filename)) @@ -140,6 +187,9 @@ def _file2dict(filename, use_predefined_variables=True): temp_config_file.name) else: shutil.copyfile(filename, temp_config_file.name) + # Substitute base variables + base_var_dict = Config._pre_substitute_base_vars( + temp_config_file.name, temp_config_file.name) if filename.endswith('.py'): temp_module_name = osp.splitext(temp_config_name)[0] @@ -184,6 +234,10 @@ def _file2dict(filename, use_predefined_variables=True): raise KeyError('Duplicate key is not allowed among bases') base_cfg_dict.update(c) + # Subtitute base variables + cfg_dict = Config._substitute_base_vars(cfg_dict, base_var_dict, + base_cfg_dict) + base_cfg_dict = Config._merge_a_into_b(cfg_dict, base_cfg_dict) cfg_dict = base_cfg_dict diff --git a/tests/data/config/t.py b/tests/data/config/t.py new file mode 100644 index 0000000000..c8c1856404 --- /dev/null +++ b/tests/data/config/t.py @@ -0,0 +1,6 @@ +_base_ = ['./l1.py', './l2.yaml', './l3.json', './l4.py'] +item3 = False +item4 = 'test' +item8 = '{{fileBasename}}' +item9 = {{ base.item2 }} +item10 = {{ base.item7.b.c }} diff --git a/tests/test_utils/test_config.py b/tests/test_utils/test_config.py index 5abafe80b8..760de83277 100644 --- a/tests/test_utils/test_config.py +++ b/tests/test_utils/test_config.py @@ -224,6 +224,24 @@ def test_merge_from_multiple_bases(): Config.fromfile(osp.join(data_path, 'config/m.py')) +def test_base_variables(): + cfg_file = osp.join(data_path, 'config/t.py') + cfg = Config.fromfile(cfg_file) + assert isinstance(cfg, Config) + assert cfg.filename == cfg_file + # cfg.field + assert cfg.item1 == [1, 2] + assert cfg.item2.a == 0 + assert cfg.item3 is False + assert cfg.item4 == 'test' + assert cfg.item5 == dict(a=0, b=1) + assert cfg.item6 == [dict(a=0), dict(b=1)] + assert cfg.item7 == dict(a=[0, 1, 2], b=dict(c=[3.1, 4.2, 5.3])) + assert cfg.item8 == 't.py' + assert cfg.item9 == dict(a=0) + assert cfg.item10 == [3.1, 4.2, 5.3] + + def test_merge_recursive_bases(): cfg_file = osp.join(data_path, 'config/f.py') cfg = Config.fromfile(cfg_file) From 6770d2b51f22bec8ca938329b776385925267989 Mon Sep 17 00:00:00 2001 From: lizz Date: Mon, 7 Jun 2021 12:18:44 +0800 Subject: [PATCH 02/10] Test json and yaml as well Signed-off-by: lizz --- tests/data/config/t.json | 13 +++++++++++++ tests/data/config/t.yaml | 6 ++++++ tests/test_utils/test_config.py | 31 ++++++++++++++++--------------- 3 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 tests/data/config/t.json create mode 100644 tests/data/config/t.yaml diff --git a/tests/data/config/t.json b/tests/data/config/t.json new file mode 100644 index 0000000000..7f8f9acf41 --- /dev/null +++ b/tests/data/config/t.json @@ -0,0 +1,13 @@ +{ + "_base_": [ + "./l1.py", + "./l2.yaml", + "./l3.json", + "./l4.py" + ], + "item3": false, + "item4": "test", + "item8": "{{fileBasename}}", + "item9": {{ base.item2 }}, + "item10": {{ base.item7.b.c }} +} diff --git a/tests/data/config/t.yaml b/tests/data/config/t.yaml new file mode 100644 index 0000000000..e979b0d9b5 --- /dev/null +++ b/tests/data/config/t.yaml @@ -0,0 +1,6 @@ +_base_ : ['./l1.py', './l2.yaml', './l3.json', './l4.py'] +item3 : False +item4 : 'test' +item8 : '{{fileBasename}}' +item9 : {{ base.item2 }} +item10 : {{ base.item7.b.c }} diff --git a/tests/test_utils/test_config.py b/tests/test_utils/test_config.py index 760de83277..2ead7dca8d 100644 --- a/tests/test_utils/test_config.py +++ b/tests/test_utils/test_config.py @@ -225,21 +225,22 @@ def test_merge_from_multiple_bases(): def test_base_variables(): - cfg_file = osp.join(data_path, 'config/t.py') - cfg = Config.fromfile(cfg_file) - assert isinstance(cfg, Config) - assert cfg.filename == cfg_file - # cfg.field - assert cfg.item1 == [1, 2] - assert cfg.item2.a == 0 - assert cfg.item3 is False - assert cfg.item4 == 'test' - assert cfg.item5 == dict(a=0, b=1) - assert cfg.item6 == [dict(a=0), dict(b=1)] - assert cfg.item7 == dict(a=[0, 1, 2], b=dict(c=[3.1, 4.2, 5.3])) - assert cfg.item8 == 't.py' - assert cfg.item9 == dict(a=0) - assert cfg.item10 == [3.1, 4.2, 5.3] + for file in ['t.py', 't.json', 't.yaml']: + cfg_file = osp.join(data_path, f'config/{file}') + cfg = Config.fromfile(cfg_file) + assert isinstance(cfg, Config) + assert cfg.filename == cfg_file + # cfg.field + assert cfg.item1 == [1, 2] + assert cfg.item2.a == 0 + assert cfg.item3 is False + assert cfg.item4 == 'test' + assert cfg.item5 == dict(a=0, b=1) + assert cfg.item6 == [dict(a=0), dict(b=1)] + assert cfg.item7 == dict(a=[0, 1, 2], b=dict(c=[3.1, 4.2, 5.3])) + assert cfg.item8 == file + assert cfg.item9 == dict(a=0) + assert cfg.item10 == [3.1, 4.2, 5.3] def test_merge_recursive_bases(): From e1d294ca41b0a4a0509a00ef135476a1d59f73af Mon Sep 17 00:00:00 2001 From: lizz Date: Mon, 7 Jun 2021 13:52:19 +0800 Subject: [PATCH 03/10] Add test for recusive base Signed-off-by: lizz --- mmcv/utils/config.py | 5 +++++ tests/data/config/u.json | 26 +++++++++++++++++++++++++ tests/data/config/u.py | 13 +++++++++++++ tests/data/config/u.yaml | 15 +++++++++++++++ tests/test_utils/test_config.py | 34 +++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+) create mode 100644 tests/data/config/u.json create mode 100644 tests/data/config/u.py create mode 100644 tests/data/config/u.yaml diff --git a/mmcv/utils/config.py b/mmcv/utils/config.py index 893b83a1f9..51d552f95f 100644 --- a/mmcv/utils/config.py +++ b/mmcv/utils/config.py @@ -164,6 +164,11 @@ def _substitute_base_vars(cfg, base_var_dict, base_cfg): Config._substitute_base_vars(c, base_var_dict, base_cfg) for c in cfg ] + elif isinstance(cfg, str) and cfg in base_var_dict: + new_v = base_cfg + for new_k in base_var_dict[cfg].split('.'): + new_v = new_v[new_k] + cfg = new_v return cfg diff --git a/tests/data/config/u.json b/tests/data/config/u.json new file mode 100644 index 0000000000..32ae0f79ca --- /dev/null +++ b/tests/data/config/u.json @@ -0,0 +1,26 @@ +{ + "_base_": [ + "./t.py" + ], + "base": "haha", + "item11": {{ base.item8 }}, + "item12": {{ base.item9 }}, + "item13": {{ base.item10 }}, + "item14": {{ base.item1 }}, + "item15": { + "a": { + "b": {{ base.item2 }} + }, + "b": [ + {{ base.item3 }} + ], + "c": [{{ base.item4 }}], + "d": [[ + { + "e": {{ base.item5.a }} + } + ], + {{ base.item6 }}], + "e": {{ base.item1 }} + } +} diff --git a/tests/data/config/u.py b/tests/data/config/u.py new file mode 100644 index 0000000000..710c229ca8 --- /dev/null +++ b/tests/data/config/u.py @@ -0,0 +1,13 @@ +_base_ = ['./t.py'] +base = 'haha' +item11 = {{ base.item8 }} +item12 = {{ base.item9 }} +item13 = {{ base.item10 }} +item14 = {{ base.item1 }} +item15 = dict( + a = dict( b = {{ base.item2 }} ), + b = [{{ base.item3 }}], + c = [{{ base.item4 }}], + d = [[dict(e = {{ base.item5.a }})],{{ base.item6 }}], + e = {{ base.item1 }} +) diff --git a/tests/data/config/u.yaml b/tests/data/config/u.yaml new file mode 100644 index 0000000000..c48a4e2ab8 --- /dev/null +++ b/tests/data/config/u.yaml @@ -0,0 +1,15 @@ +_base_: ["./t.py"] +base: "haha" +item11: {{ base.item8 }} +item12: {{ base.item9 }} +item13: {{ base.item10 }} +item14: {{ base.item1 }} +item15: + a: + b: {{ base.item2 }} + b: [{{ base.item3 }}] + c: [{{ base.item4 }}] + d: + - [e: {{ base.item5.a }}] + - {{ base.item6 }} + e: {{ base.item1 }} diff --git a/tests/test_utils/test_config.py b/tests/test_utils/test_config.py index 2ead7dca8d..df2adca23f 100644 --- a/tests/test_utils/test_config.py +++ b/tests/test_utils/test_config.py @@ -242,6 +242,40 @@ def test_base_variables(): assert cfg.item9 == dict(a=0) assert cfg.item10 == [3.1, 4.2, 5.3] + # test nested base + for file in ['u.py', 'u.json', 'u.yaml']: + cfg_file = osp.join(data_path, f'config/{file}') + cfg = Config.fromfile(cfg_file) + assert isinstance(cfg, Config) + assert cfg.filename == cfg_file + # cfg.field + assert cfg.item1 == [1, 2] + assert cfg.item2.a == 0 + assert cfg.item3 is False + assert cfg.item4 == 'test' + assert cfg.item5 == dict(a=0, b=1) + assert cfg.item6 == [dict(a=0), dict(b=1)] + assert cfg.item7 == dict(a=[0, 1, 2], b=dict(c=[3.1, 4.2, 5.3])) + assert cfg.item8 == 't.py' + assert cfg.item9 == dict(a=0) + assert cfg.item10 == [3.1, 4.2, 5.3] + assert cfg.item11 == 't.py' + assert cfg.item12 == dict(a=0) + assert cfg.item13 == [3.1, 4.2, 5.3] + assert cfg.item14 == [1, 2] + assert cfg.item15 == dict( + a=dict(b=dict(a=0)), + b=[False], + c=['test'], + d=[[{ + 'e': 0 + }], [{ + 'a': 0 + }, { + 'b': 1 + }]], + e=[1, 2]) + def test_merge_recursive_bases(): cfg_file = osp.join(data_path, 'config/f.py') From a4b77ae9e8c503b00d1a365a81ce9419a5421ec5 Mon Sep 17 00:00:00 2001 From: lizz Date: Mon, 7 Jun 2021 13:55:57 +0800 Subject: [PATCH 04/10] Test misleading values Signed-off-by: lizz --- tests/data/config/u.json | 2 +- tests/data/config/u.py | 2 +- tests/data/config/u.yaml | 2 +- tests/test_utils/test_config.py | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/data/config/u.json b/tests/data/config/u.json index 32ae0f79ca..84704aeb50 100644 --- a/tests/data/config/u.json +++ b/tests/data/config/u.json @@ -2,7 +2,7 @@ "_base_": [ "./t.py" ], - "base": "haha", + "base": "base.item8", "item11": {{ base.item8 }}, "item12": {{ base.item9 }}, "item13": {{ base.item10 }}, diff --git a/tests/data/config/u.py b/tests/data/config/u.py index 710c229ca8..3a20c5aa1e 100644 --- a/tests/data/config/u.py +++ b/tests/data/config/u.py @@ -1,5 +1,5 @@ _base_ = ['./t.py'] -base = 'haha' +base = 'base.item8' item11 = {{ base.item8 }} item12 = {{ base.item9 }} item13 = {{ base.item10 }} diff --git a/tests/data/config/u.yaml b/tests/data/config/u.yaml index c48a4e2ab8..2a6c176366 100644 --- a/tests/data/config/u.yaml +++ b/tests/data/config/u.yaml @@ -1,5 +1,5 @@ _base_: ["./t.py"] -base: "haha" +base: "base.item8" item11: {{ base.item8 }} item12: {{ base.item9 }} item13: {{ base.item10 }} diff --git a/tests/test_utils/test_config.py b/tests/test_utils/test_config.py index df2adca23f..0f64b9c435 100644 --- a/tests/test_utils/test_config.py +++ b/tests/test_utils/test_config.py @@ -249,6 +249,7 @@ def test_base_variables(): assert isinstance(cfg, Config) assert cfg.filename == cfg_file # cfg.field + assert cfg.base == 'base.item8' assert cfg.item1 == [1, 2] assert cfg.item2.a == 0 assert cfg.item3 is False From eb319f583813c471972a606d6eb2127da87c7c17 Mon Sep 17 00:00:00 2001 From: lizz Date: Mon, 7 Jun 2021 14:09:19 +0800 Subject: [PATCH 05/10] Improve comments Signed-off-by: lizz --- mmcv/utils/config.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mmcv/utils/config.py b/mmcv/utils/config.py index 51d552f95f..cd528750ad 100644 --- a/mmcv/utils/config.py +++ b/mmcv/utils/config.py @@ -143,6 +143,7 @@ def _pre_substitute_base_vars(filename, temp_config_name): @staticmethod def _substitute_base_vars(cfg, base_var_dict, base_cfg): + """Substitute variable strings to their actual values.""" cfg = copy.deepcopy(cfg) if isinstance(cfg, dict): @@ -192,7 +193,7 @@ def _file2dict(filename, use_predefined_variables=True): temp_config_file.name) else: shutil.copyfile(filename, temp_config_file.name) - # Substitute base variables + # Substitute base variables from placeholders to strings base_var_dict = Config._pre_substitute_base_vars( temp_config_file.name, temp_config_file.name) @@ -239,7 +240,7 @@ def _file2dict(filename, use_predefined_variables=True): raise KeyError('Duplicate key is not allowed among bases') base_cfg_dict.update(c) - # Subtitute base variables + # Subtitute base variables from strings to their actual values cfg_dict = Config._substitute_base_vars(cfg_dict, base_var_dict, base_cfg_dict) From 8625c0e947c3bbb8e5bd6718ec7556932d06f243 Mon Sep 17 00:00:00 2001 From: lizz Date: Mon, 7 Jun 2021 14:15:47 +0800 Subject: [PATCH 06/10] Add doc Signed-off-by: lizz --- docs/utils.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/utils.md b/docs/utils.md index 991a862f97..45a42df5c8 100644 --- a/docs/utils.md +++ b/docs/utils.md @@ -154,6 +154,22 @@ _base_ = ['./config_a.py', './config_e.py'] ... d='string') ``` +#### Reference variables from base + +You can reference variables defined in base using the following grammar. + +`base.py` + +```python +item1 = 'a' +item2 = dict(item3 = 'b') +``` + +```python +_base_ = ['./base.py'] +item = dict(a = {{ base.item1 }}, b = {{ base.item2.item3 }}) +``` + ### ProgressBar If you want to apply a method to a list of items and track the progress, `track_progress` From 81319740b33d61ee2c7a9de45079c6e2e718bad5 Mon Sep 17 00:00:00 2001 From: lizz Date: Mon, 7 Jun 2021 14:22:30 +0800 Subject: [PATCH 07/10] Improve doc Signed-off-by: lizz --- docs/utils.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/utils.md b/docs/utils.md index 45a42df5c8..5292374eab 100644 --- a/docs/utils.md +++ b/docs/utils.md @@ -165,11 +165,21 @@ item1 = 'a' item2 = dict(item3 = 'b') ``` +`config_g.py` + ```python _base_ = ['./base.py'] item = dict(a = {{ base.item1 }}, b = {{ base.item2.item3 }}) ``` +```python +>>> cfg = Config.fromfile('./config_g.py') +>>> print(cfg.pretty_text) +item1 = 'a' +item2 = dict(item3='b') +item = dict(a='a', b='b') +``` + ### ProgressBar If you want to apply a method to a list of items and track the progress, `track_progress` From 0e8d373c150da2784a645c785d7f6f9b8619e3bd Mon Sep 17 00:00:00 2001 From: lizz Date: Wed, 9 Jun 2021 10:50:07 +0800 Subject: [PATCH 08/10] More tests Signed-off-by: lizz --- tests/data/config/v.py | 12 ++++++++++++ tests/test_utils/test_config.py | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 tests/data/config/v.py diff --git a/tests/data/config/v.py b/tests/data/config/v.py new file mode 100644 index 0000000000..11d7433ceb --- /dev/null +++ b/tests/data/config/v.py @@ -0,0 +1,12 @@ +_base_ = ['./u.py'] +base = 'base.item8' +item21 = {{ base.item11 }} +item22 = item21 +item23 = {{ base.item10 }} +item24 = item23 +item25 = dict( + a = dict( b = item24 ), + b = [item24], + c = [[dict(e = item22)],{{ base.item6 }}], + e = item21 +) diff --git a/tests/test_utils/test_config.py b/tests/test_utils/test_config.py index 0f64b9c435..d48fda03dc 100644 --- a/tests/test_utils/test_config.py +++ b/tests/test_utils/test_config.py @@ -277,6 +277,27 @@ def test_base_variables(): }]], e=[1, 2]) + # test reference assignment for py + cfg_file = osp.join(data_path, 'config/v.py') + cfg = Config.fromfile(cfg_file) + assert isinstance(cfg, Config) + assert cfg.filename == cfg_file + assert cfg.item21 == 't.py' + assert cfg.item22 == 't.py' + assert cfg.item23 == [3.1, 4.2, 5.3] + assert cfg.item24 == [3.1, 4.2, 5.3] + assert cfg.item25 == dict( + a=dict(b=[3.1, 4.2, 5.3]), + b=[[3.1, 4.2, 5.3]], + c=[[{ + 'e': 't.py' + }], [{ + 'a': 0 + }, { + 'b': 1 + }]], + e='t.py') + def test_merge_recursive_bases(): cfg_file = osp.join(data_path, 'config/f.py') From bf1d252bc5e7157c7e1976b373644edeabdf35aa Mon Sep 17 00:00:00 2001 From: lizz Date: Wed, 9 Jun 2021 11:28:27 +0800 Subject: [PATCH 09/10] Harder test case Signed-off-by: lizz --- tests/data/config/v.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/data/config/v.py b/tests/data/config/v.py index 11d7433ceb..e45f7642df 100644 --- a/tests/data/config/v.py +++ b/tests/data/config/v.py @@ -1,5 +1,4 @@ -_base_ = ['./u.py'] -base = 'base.item8' +_base_ = base = ['./u.py'] item21 = {{ base.item11 }} item22 = item21 item23 = {{ base.item10 }} From 2f4ac79d4e764ca478c9e15e21deff8e88489b04 Mon Sep 17 00:00:00 2001 From: lizz Date: Wed, 16 Jun 2021 14:30:39 +0800 Subject: [PATCH 10/10] use BASE_KEY instead of base Signed-off-by: lizz --- docs/utils.md | 2 +- mmcv/utils/config.py | 4 ++-- tests/data/config/t.json | 4 ++-- tests/data/config/t.py | 4 ++-- tests/data/config/t.yaml | 4 ++-- tests/data/config/u.json | 22 +++++++++++----------- tests/data/config/u.py | 20 ++++++++++---------- tests/data/config/u.yaml | 22 +++++++++++----------- tests/data/config/v.py | 8 ++++---- tests/test_utils/test_config.py | 2 +- 10 files changed, 46 insertions(+), 46 deletions(-) diff --git a/docs/utils.md b/docs/utils.md index 5292374eab..84d6b06e23 100644 --- a/docs/utils.md +++ b/docs/utils.md @@ -169,7 +169,7 @@ item2 = dict(item3 = 'b') ```python _base_ = ['./base.py'] -item = dict(a = {{ base.item1 }}, b = {{ base.item2.item3 }}) +item = dict(a = {{ _base_.item1 }}, b = {{ _base_.item2.item3 }}) ``` ```python diff --git a/mmcv/utils/config.py b/mmcv/utils/config.py index cd528750ad..97769c5368 100644 --- a/mmcv/utils/config.py +++ b/mmcv/utils/config.py @@ -130,12 +130,12 @@ def _pre_substitute_base_vars(filename, temp_config_name): # Setting encoding explicitly to resolve coding issue on windows config_file = f.read() base_var_dict = {} - regexp = r'\{\{\s*base\.([\w\.]+)\s*\}\}' + regexp = r'\{\{\s*' + BASE_KEY + r'\.([\w\.]+)\s*\}\}' base_vars = set(re.findall(regexp, config_file)) for base_var in base_vars: randstr = f'_{base_var}_{uuid.uuid4().hex.lower()[:6]}' base_var_dict[randstr] = base_var - regexp = r'\{\{\s*base\.' + base_var + r'\s*\}\}' + regexp = r'\{\{\s*' + BASE_KEY + r'\.' + base_var + r'\s*\}\}' config_file = re.sub(regexp, f'"{randstr}"', config_file) with open(temp_config_name, 'w') as tmp_config_file: tmp_config_file.write(config_file) diff --git a/tests/data/config/t.json b/tests/data/config/t.json index 7f8f9acf41..8f7b9b4a17 100644 --- a/tests/data/config/t.json +++ b/tests/data/config/t.json @@ -8,6 +8,6 @@ "item3": false, "item4": "test", "item8": "{{fileBasename}}", - "item9": {{ base.item2 }}, - "item10": {{ base.item7.b.c }} + "item9": {{ _base_.item2 }}, + "item10": {{ _base_.item7.b.c }} } diff --git a/tests/data/config/t.py b/tests/data/config/t.py index c8c1856404..9f085ae675 100644 --- a/tests/data/config/t.py +++ b/tests/data/config/t.py @@ -2,5 +2,5 @@ item3 = False item4 = 'test' item8 = '{{fileBasename}}' -item9 = {{ base.item2 }} -item10 = {{ base.item7.b.c }} +item9 = {{ _base_.item2 }} +item10 = {{ _base_.item7.b.c }} diff --git a/tests/data/config/t.yaml b/tests/data/config/t.yaml index e979b0d9b5..ab42859ec9 100644 --- a/tests/data/config/t.yaml +++ b/tests/data/config/t.yaml @@ -2,5 +2,5 @@ _base_ : ['./l1.py', './l2.yaml', './l3.json', './l4.py'] item3 : False item4 : 'test' item8 : '{{fileBasename}}' -item9 : {{ base.item2 }} -item10 : {{ base.item7.b.c }} +item9 : {{ _base_.item2 }} +item10 : {{ _base_.item7.b.c }} diff --git a/tests/data/config/u.json b/tests/data/config/u.json index 84704aeb50..f6a01e3c08 100644 --- a/tests/data/config/u.json +++ b/tests/data/config/u.json @@ -2,25 +2,25 @@ "_base_": [ "./t.py" ], - "base": "base.item8", - "item11": {{ base.item8 }}, - "item12": {{ base.item9 }}, - "item13": {{ base.item10 }}, - "item14": {{ base.item1 }}, + "base": "_base_.item8", + "item11": {{ _base_.item8 }}, + "item12": {{ _base_.item9 }}, + "item13": {{ _base_.item10 }}, + "item14": {{ _base_.item1 }}, "item15": { "a": { - "b": {{ base.item2 }} + "b": {{ _base_.item2 }} }, "b": [ - {{ base.item3 }} + {{ _base_.item3 }} ], - "c": [{{ base.item4 }}], + "c": [{{ _base_.item4 }}], "d": [[ { - "e": {{ base.item5.a }} + "e": {{ _base_.item5.a }} } ], - {{ base.item6 }}], - "e": {{ base.item1 }} + {{ _base_.item6 }}], + "e": {{ _base_.item1 }} } } diff --git a/tests/data/config/u.py b/tests/data/config/u.py index 3a20c5aa1e..bdd96a7e46 100644 --- a/tests/data/config/u.py +++ b/tests/data/config/u.py @@ -1,13 +1,13 @@ _base_ = ['./t.py'] -base = 'base.item8' -item11 = {{ base.item8 }} -item12 = {{ base.item9 }} -item13 = {{ base.item10 }} -item14 = {{ base.item1 }} +base = '_base_.item8' +item11 = {{ _base_.item8 }} +item12 = {{ _base_.item9 }} +item13 = {{ _base_.item10 }} +item14 = {{ _base_.item1 }} item15 = dict( - a = dict( b = {{ base.item2 }} ), - b = [{{ base.item3 }}], - c = [{{ base.item4 }}], - d = [[dict(e = {{ base.item5.a }})],{{ base.item6 }}], - e = {{ base.item1 }} + a = dict( b = {{ _base_.item2 }} ), + b = [{{ _base_.item3 }}], + c = [{{ _base_.item4 }}], + d = [[dict(e = {{ _base_.item5.a }})],{{ _base_.item6 }}], + e = {{ _base_.item1 }} ) diff --git a/tests/data/config/u.yaml b/tests/data/config/u.yaml index 2a6c176366..d201cb926d 100644 --- a/tests/data/config/u.yaml +++ b/tests/data/config/u.yaml @@ -1,15 +1,15 @@ _base_: ["./t.py"] -base: "base.item8" -item11: {{ base.item8 }} -item12: {{ base.item9 }} -item13: {{ base.item10 }} -item14: {{ base.item1 }} +base: "_base_.item8" +item11: {{ _base_.item8 }} +item12: {{ _base_.item9 }} +item13: {{ _base_.item10 }} +item14: {{ _base_.item1 }} item15: a: - b: {{ base.item2 }} - b: [{{ base.item3 }}] - c: [{{ base.item4 }}] + b: {{ _base_.item2 }} + b: [{{ _base_.item3 }}] + c: [{{ _base_.item4 }}] d: - - [e: {{ base.item5.a }}] - - {{ base.item6 }} - e: {{ base.item1 }} + - [e: {{ _base_.item5.a }}] + - {{ _base_.item6 }} + e: {{ _base_.item1 }} diff --git a/tests/data/config/v.py b/tests/data/config/v.py index e45f7642df..3d2a1a436c 100644 --- a/tests/data/config/v.py +++ b/tests/data/config/v.py @@ -1,11 +1,11 @@ -_base_ = base = ['./u.py'] -item21 = {{ base.item11 }} +_base_ = ['./u.py'] +item21 = {{ _base_.item11 }} item22 = item21 -item23 = {{ base.item10 }} +item23 = {{ _base_.item10 }} item24 = item23 item25 = dict( a = dict( b = item24 ), b = [item24], - c = [[dict(e = item22)],{{ base.item6 }}], + c = [[dict(e = item22)],{{ _base_.item6 }}], e = item21 ) diff --git a/tests/test_utils/test_config.py b/tests/test_utils/test_config.py index d48fda03dc..44a67ba500 100644 --- a/tests/test_utils/test_config.py +++ b/tests/test_utils/test_config.py @@ -249,7 +249,7 @@ def test_base_variables(): assert isinstance(cfg, Config) assert cfg.filename == cfg_file # cfg.field - assert cfg.base == 'base.item8' + assert cfg.base == '_base_.item8' assert cfg.item1 == [1, 2] assert cfg.item2.a == 0 assert cfg.item3 is False