diff --git a/mmcv/utils/registry.py b/mmcv/utils/registry.py index 7f96b6cc67..5894ad25f2 100644 --- a/mmcv/utils/registry.py +++ b/mmcv/utils/registry.py @@ -175,5 +175,8 @@ def build_from_cfg(cfg, registry, default_args=None): else: raise TypeError( f'type must be a str or valid type, but got {type(obj_type)}') - - return obj_cls(**args) + try: + return obj_cls(**args) + except Exception as e: + # Normal TypeError does not print class name. + raise type(e)(f'{obj_cls.__name__}: {e}') diff --git a/tests/test_utils/test_registry.py b/tests/test_utils/test_registry.py index 3106c39e56..33d49c5f48 100644 --- a/tests/test_utils/test_registry.py +++ b/tests/test_utils/test_registry.py @@ -221,3 +221,8 @@ def __init__(self, depth, stages=4): with pytest.raises(TypeError): cfg = dict(type='ResNet', depth=50) model = mmcv.build_from_cfg(cfg, BACKBONES, default_args=0) + + # incorrect arguments + with pytest.raises(TypeError): + cfg = dict(type='ResNet', non_existing_arg=50) + model = mmcv.build_from_cfg(cfg, BACKBONES)