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

Support deprecation info in Config #1275

Merged
merged 5 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions docs/understand_mmcv/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,24 @@ item1 = 'a'
item2 = dict(item3='b')
item = dict(a='a', b='b')
```

### Add deprecation information in configs

Deprecation information can be added in a config file, which will trigger a `UserWarning` when this config file is loaded.

`deprecated_cfg.py`

```python
_base_ = 'expected_cfg.py'

_deprecation_ = dict(
expected = 'expected_cfg.py', # optional to show expected config path in the warning information
reference = 'url to related PR' # optional to show reference link in the warning information
)
```

```python
>>> cfg = Config.fromfile('./deprecated_cfg.py')

UserWarning: The config file deprecated.py will be deprecated in the future. Please use expected_cfg.py instead. More information can be found at https://github.com/open-mmlab/mmcv/pull/1275
```
14 changes: 14 additions & 0 deletions mmcv/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

BASE_KEY = '_base_'
DELETE_KEY = '_delete_'
DEPRECATION_KEY = '_deprecation_'
RESERVED_KEYS = ['filename', 'text', 'pretty_text']


Expand Down Expand Up @@ -217,6 +218,19 @@ def _file2dict(filename, use_predefined_variables=True):
# close temp file
temp_config_file.close()

# check deprecation information
if DEPRECATION_KEY in cfg_dict:
deprecation_info = cfg_dict.pop(DEPRECATION_KEY)
warning_msg = f'The config file {filename} will be deprecated ' \
'in the future.'
if 'expected' in deprecation_info:
warning_msg += f' Please use {deprecation_info["expected"]} ' \
'instead.'
if 'reference' in deprecation_info:
warning_msg += ' More information can be found at ' \
f'{deprecation_info["reference"]}'
warnings.warn(warning_msg)

cfg_text = filename + '\n'
with open(filename, 'r', encoding='utf-8') as f:
# Setting encoding explicitly to resolve coding issue on windows
Expand Down
5 changes: 5 additions & 0 deletions tests/data/config/deprecated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_base_ = './expected.py'

_deprecation_ = dict(
expected='tests/data/config/expected.py',
reference='https://github.com/open-mmlab/mmcv/pull/1275')
1 change: 1 addition & 0 deletions tests/data/config/deprecated_as_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_base_ = './deprecated.py'
1 change: 1 addition & 0 deletions tests/data/config/expected.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
item1 = 'expected'
12 changes: 12 additions & 0 deletions tests/test_utils/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,15 @@ def test_pickle_support():
pkl_cfg = load(pkl_cfg_filename)

assert pkl_cfg._cfg_dict == cfg._cfg_dict


def test_deprecation():
deprecated_cfg_files = [
osp.join(data_path, 'config/deprecated.py'),
osp.join(data_path, 'config/deprecated_as_base.py')
]

for cfg_file in deprecated_cfg_files:
with pytest.warns(UserWarning):
cfg = Config.fromfile(cfg_file)
assert cfg.item1 == 'expected'