Skip to content
Snippets Groups Projects
Unverified Commit b3d54481 authored by Zaida Zhou's avatar Zaida Zhou Committed by GitHub
Browse files

[Fix] Fallback to use self registry if default_scope not found (#122)

parent 72cf4109
No related branches found
No related tags found
No related merge requests found
# Copyright (c) OpenMMLab. All rights reserved. # Copyright (c) OpenMMLab. All rights reserved.
import inspect import inspect
import sys import sys
import warnings
from collections.abc import Callable from collections.abc import Callable
from typing import Any, Dict, List, Optional, Tuple, Type, Union from typing import Any, Dict, List, Optional, Tuple, Type, Union
...@@ -382,8 +383,14 @@ class Registry: ...@@ -382,8 +383,14 @@ class Registry:
root = self._get_root_registry() root = self._get_root_registry()
registry = root._search_child(default_scope) registry = root._search_child(default_scope)
if registry is None: if registry is None:
raise KeyError( # if `default_scope` can not be found, fallback to use self
f'{default_scope} does not exist in the registry tree.') warnings.warn(
f'Failed to search registry named "{default_scope}". '
'As a workaround, the current registry is used to build '
'instance. This may cause unexpected failure when running '
'the built modules. Please check whether '
f'"{default_scope}" is a correct scope.')
registry = self
else: else:
registry = self registry = self
......
...@@ -341,14 +341,14 @@ class TestRegistry: ...@@ -341,14 +341,14 @@ class TestRegistry:
assert isinstance(MID_HOUNDS.build(b_cfg), Beagle) assert isinstance(MID_HOUNDS.build(b_cfg), Beagle)
# test `default_scope` # test `default_scope`
# `default_scope` is an invalid scope
with pytest.raises(KeyError):
LITTLE_HOUNDS.build(b_cfg, default_scope='invalid_mid_hound')
# switch the current registry to another registry # switch the current registry to another registry
dog = LITTLE_HOUNDS.build(b_cfg, default_scope='mid_hound') dog = LITTLE_HOUNDS.build(b_cfg, default_scope='mid_hound')
assert isinstance(dog, Beagle) assert isinstance(dog, Beagle)
# `default_scope` can not be found
dog = MID_HOUNDS.build(b_cfg, default_scope='scope-not-found')
assert isinstance(dog, Beagle)
def test_repr(self): def test_repr(self):
CATS = Registry('cat') CATS = Registry('cat')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment