Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhance] Call register_all_modules in Registry.get() #541

Merged
merged 7 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 36 additions & 3 deletions mmengine/registry/registry.py
Original file line number Diff line number Diff line change
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 mannuly.',
HAOCHENYE marked this conversation as resolved.
Show resolved Hide resolved
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} mannuly.',
HAOCHENYE marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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