Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Fix the logic of setting lazy_import #1239

Merged
merged 6 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions mmengine/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,11 @@ def fromfile(filename: Union[str, Path],
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:
inferred_lazy_import = Config._is_lazy_import(filename)
if not inferred_lazy_import or lazy_import is False:
zhouzaida marked this conversation as resolved.
Show resolved Hide resolved
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'])
Expand Down Expand Up @@ -794,18 +794,22 @@ def _substitute_base_vars(cfg: Any, base_var_dict: dict,
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 '
Expand Down Expand Up @@ -852,7 +856,9 @@ def _file2dict(
_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()
Expand Down
2 changes: 1 addition & 1 deletion tests/data/config/lazy_module_config/error_mix_using1.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Copyright (c) OpenMMLab. All rights reserved.
_base_ = './toy_model.py'
_base_ = './error_mix_using3.py'
2 changes: 2 additions & 0 deletions tests/data/config/lazy_module_config/error_mix_using3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright (c) OpenMMLab. All rights reserved.
import numpy as np
zhouzaida marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions tests/test_config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,12 @@ def _compare_dict(a, b):
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(
Expand Down