Skip to content

Commit

Permalink
[Enhance] Call register_all_modules in Registry.get() (open-mmlab…
Browse files Browse the repository at this point in the history
…#541)

* call register_all_modules  in Registry.get()

* Fix ci

* fix scope bug (scope_name -> scope), for temp sync

* Fix unit test

* Refine log information

Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>

* Fix typo

Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
  • Loading branch information
2 people authored and C1rN09 committed Nov 1, 2022
1 parent 23310c4 commit b819d33
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
39 changes: 36 additions & 3 deletions mmengine/registry/registry.py
Expand Up @@ -243,9 +243,24 @@ def switch_scope_and_registry(self, scope: str) -> Generator:
if scope_name in PKG2PROJECT:
try:
module = import_module(f'{scope_name}.utils')
module.register_all_modules() # type: ignore
except ImportError as e:
raise e
module.register_all_modules(False) # type: ignore
except (ImportError, AttributeError, ModuleNotFoundError):
if scope in PKG2PROJECT:
print_log(
f'{scope} is not installed and its '
'modules will not be registered. If you '
'want to use modules defined in '
f'{scope}, Please install {scope} by '
f'`pip install {PKG2PROJECT[scope]}.',
logger='current',
level=logging.WARNING)
else:
print_log(
f'Failed to import {scope} and register '
'its modules, please make sure you '
'have registered the module manually.',
logger='current',
level=logging.WARNING)
root = self._get_root_registry()
registry = root._search_child(scope_name)
if registry is None:
Expand Down Expand Up @@ -347,6 +362,24 @@ def get(self, key: str) -> Optional[Type]:
break
parent = parent.parent
else:
try:
module = import_module(f'{scope}.utils')
module.register_all_modules(False) # type: ignore
except (ImportError, AttributeError, ModuleNotFoundError):
if scope in PKG2PROJECT:
print_log(
f'{scope} is not installed and its modules '
'will not be registered. If you want to use '
f'modules defined in {scope}, Please install '
f'{scope} by `pip install {PKG2PROJECT[scope]} ',
logger='current',
level=logging.WARNING)
else:
print_log(
f'Failed to import "{scope}", and register its '
f'modules. Please register {real_key} manually.',
logger='current',
level=logging.WARNING)
# get from self._children
if scope in self._children:
obj_cls = self._children[scope].get(real_key)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_registry/test_registry.py
Expand Up @@ -419,7 +419,7 @@ class YourSamoyed:
assert isinstance(dog.friend, YourSamoyed)
assert DefaultScope.get_current_instance().scope_name != 'samoyed'

def test_get_registry_by_scope(self):
def test_switch_scope_and_registry(self):
DOGS = Registry('dogs')
HOUNDS = Registry('hounds', scope='hound', parent=DOGS)
SAMOYEDS = Registry('samoyeds', scope='samoyed', parent=DOGS)
Expand Down

0 comments on commit b819d33

Please sign in to comment.