From aae512eb9e21709a85178be53aee7a280b53827e Mon Sep 17 00:00:00 2001 From: HAOCHENYE <21724054@zju.edu.cn> Date: Thu, 6 Apr 2023 15:15:09 +0800 Subject: [PATCH] Refine debug message --- mmengine/registry/build_functions.py | 23 ++++++++++++----------- tests/test_registry/test_registry.py | 5 +++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/mmengine/registry/build_functions.py b/mmengine/registry/build_functions.py index 9c28c571c0..254356fef9 100644 --- a/mmengine/registry/build_functions.py +++ b/mmengine/registry/build_functions.py @@ -121,17 +121,18 @@ def build_from_cfg( else: obj = obj_cls(**args) # type: ignore - # For some rare cases (e.g. obj_cls is a partial function), obj_cls - # doesn't have the following attributes. Use default value to - # prevent error - cls_name = getattr(obj_cls, '__name__', str(obj_cls)) - cls_module = getattr(obj_cls, '__module__', 'unknown') - print_log( - f'An `{cls_name}` instance is built from ' # type: ignore # noqa: E501 - 'registry, its implementation can be found in ' - f'{cls_module}', # type: ignore - logger='current', - level=logging.DEBUG) + if (inspect.isclass(obj_cls) or inspect.isfunction(obj_cls) + or inspect.ismethod(obj_cls)): + print_log( + f'An `{obj_cls.__name__}` instance is built from ' # type: ignore # noqa: E501 + 'registry, and its implementation can be found in ' + f'{obj_cls.__module__}', # type: ignore + logger='current', + level=logging.DEBUG) + else: + print_log( + 'An instance is built from registry, and its constructor ' + f'is {obj_cls}') return obj except Exception as e: diff --git a/tests/test_registry/test_registry.py b/tests/test_registry/test_registry.py index 9ea4e4e22f..7deeb2e070 100644 --- a/tests/test_registry/test_registry.py +++ b/tests/test_registry/test_registry.py @@ -672,6 +672,11 @@ def forward(self, x): assert isinstance(model[1], ResNeXt) assert model[1].depth == 50 and model[1].stages == 3 + NEW_FUNCTIONS = Registry('functions', scope='new') + NEW_FUNCTIONS.register_module(module=lambda x: x, name='lambda_function') + cfg = dict(type='lambda_function', x=1) + assert NEW_FUNCTIONS.build(cfg) == 1 + # test inherit `build_func` from parent NEW_MODELS = Registry('models', parent=BACKBONES, scope='new') assert NEW_MODELS.build_func is build_model_from_cfg