Unverified Commit fa22d9db authored by Shilong Zhang's avatar Shilong Zhang Committed by GitHub
Browse files

Fix potiential bug in `deprecated_api_warning` (#1395)



* assert both dep and new args exist at same time

* add unintest

* Update tests/test_utils/test_misc.py
Co-authored-by: default avatarZaida Zhou <58739961+zhouzaida@users.noreply.github.com>

* Update tests/test_utils/test_misc.py
Co-authored-by: default avatarZaida Zhou <58739961+zhouzaida@users.noreply.github.com>

* polish warning

* polish warning
Co-authored-by: default avatarZaida Zhou <58739961+zhouzaida@users.noreply.github.com>
parent 63a6cbe9
...@@ -320,6 +320,16 @@ def deprecated_api_warning(name_dict, cls_name=None): ...@@ -320,6 +320,16 @@ def deprecated_api_warning(name_dict, cls_name=None):
if kwargs: if kwargs:
for src_arg_name, dst_arg_name in name_dict.items(): for src_arg_name, dst_arg_name in name_dict.items():
if src_arg_name in kwargs: if src_arg_name in kwargs:
assert dst_arg_name not in kwargs, (
f'The expected behavior is to replace '
f'the deprecated key `{src_arg_name}` to '
f'new key `{dst_arg_name}`, but got them '
f'in the arguments at the same time, which '
f'is confusing. `{src_arg_name} will be '
f'deprecated in the future, please '
f'use `{dst_arg_name}` instead.')
warnings.warn( warnings.warn(
f'"{src_arg_name}" is deprecated in ' f'"{src_arg_name}" is deprecated in '
f'`{func_name}`, please use "{dst_arg_name}" ' f'`{func_name}`, please use "{dst_arg_name}" '
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import pytest import pytest
import mmcv import mmcv
from mmcv import deprecated_api_warning
def test_to_ntuple(): def test_to_ntuple():
...@@ -190,3 +191,19 @@ def test_is_method_overridden(): ...@@ -190,3 +191,19 @@ def test_is_method_overridden():
base_instance = Base() base_instance = Base()
with pytest.raises(AssertionError): with pytest.raises(AssertionError):
mmcv.is_method_overridden('foo1', base_instance, sub_instance) mmcv.is_method_overridden('foo1', base_instance, sub_instance)
def test_deprecated_api_warning():
@deprecated_api_warning(name_dict=dict(old_key='new_key'))
def dummy_func(new_key=1):
return new_key
# replace `old_key` to `new_key`
assert dummy_func(old_key=2) == 2
# The expected behavior is to replace the
# deprecated key `old_key` to `new_key`,
# but got them in the arguments at the same time
with pytest.raises(AssertionError):
dummy_func(old_key=1, new_key=2)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment