diff --git a/mmengine/config/config.py b/mmengine/config/config.py index 7d9e9423..33ae816f 100644 --- a/mmengine/config/config.py +++ b/mmengine/config/config.py @@ -444,11 +444,11 @@ class Config: Config: Config instance built from config file. """ filename = str(filename) if isinstance(filename, Path) else filename - if lazy_import is None: - lazy_import = Config._is_lazy_import(filename) - if not lazy_import: + if lazy_import is False or \ + lazy_import is None and not Config._is_lazy_import(filename): cfg_dict, cfg_text, env_variables = Config._file2dict( - filename, use_predefined_variables, use_environment_variables) + filename, use_predefined_variables, use_environment_variables, + lazy_import) if import_custom_modules and cfg_dict.get('custom_imports', None): try: import_modules_from_strings(**cfg_dict['custom_imports']) @@ -813,18 +813,22 @@ class Config: def _file2dict( filename: str, use_predefined_variables: bool = True, - use_environment_variables: bool = True) -> Tuple[dict, str, dict]: + use_environment_variables: bool = True, + lazy_import: Optional[bool] = None) -> Tuple[dict, str, dict]: """Transform file to variables dictionary. Args: filename (str): Name of config file. use_predefined_variables (bool, optional): Whether to use predefined variables. Defaults to True. + lazy_import (bool): Whether to load config in `lazy_import` mode. + If it is `None`, it will be deduced by the content of the + config file. Defaults to None. Returns: Tuple[dict, str]: Variables dictionary and text of Config. """ - if Config._is_lazy_import(filename): + if lazy_import is None and Config._is_lazy_import(filename): raise RuntimeError( 'The configuration file type in the inheritance chain ' 'must match the current configuration file type, either ' @@ -871,7 +875,9 @@ class Config: _cfg_dict, _cfg_text, _env_variables = Config._file2dict( filename=base_cfg_path, use_predefined_variables=use_predefined_variables, - use_environment_variables=use_environment_variables) + use_environment_variables=use_environment_variables, + lazy_import=lazy_import, + ) cfg_text_list.append(_cfg_text) env_variables.update(_env_variables) duplicate_keys = base_cfg_dict.keys() & _cfg_dict.keys() diff --git a/tests/data/config/lazy_module_config/error_mix_using1.py b/tests/data/config/lazy_module_config/error_mix_using1.py index 7328294a..b7017ef0 100644 --- a/tests/data/config/lazy_module_config/error_mix_using1.py +++ b/tests/data/config/lazy_module_config/error_mix_using1.py @@ -1,2 +1,2 @@ # Copyright (c) OpenMMLab. All rights reserved. -_base_ = './toy_model.py' +_base_ = './error_mix_using3.py' diff --git a/tests/data/config/lazy_module_config/error_mix_using3.py b/tests/data/config/lazy_module_config/error_mix_using3.py new file mode 100644 index 00000000..70418146 --- /dev/null +++ b/tests/data/config/lazy_module_config/error_mix_using3.py @@ -0,0 +1,2 @@ +# Copyright (c) OpenMMLab. All rights reserved. +import numpy as np diff --git a/tests/test_config/test_config.py b/tests/test_config/test_config.py index 660d6b0a..474a7d7b 100644 --- a/tests/test_config/test_config.py +++ b/tests/test_config/test_config.py @@ -1044,6 +1044,12 @@ error_attr = mmengine.error_attr osp.join(self.data_path, 'config/lazy_module_config/error_mix_using1.py')) + # Force to import in non-lazy-import mode + Config.fromfile( + osp.join(self.data_path, + 'config/lazy_module_config/error_mix_using1.py'), + lazy_import=False) + # current lazy-import config, base text config with pytest.raises(RuntimeError, match='_base_ ='): Config.fromfile(