Skip to content

Commit

Permalink
revert changes
Browse files Browse the repository at this point in the history
  • Loading branch information
HAOCHENYE committed Jul 28, 2023
1 parent 0e50909 commit 7a79153
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
22 changes: 15 additions & 7 deletions mmengine/registry/build_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def build_from_cfg(
obj_cls = registry.get(obj_type)
if obj_cls is None:
raise KeyError(
f'{obj_type} is not in the {registry.name} registry. '
f'{obj_type} is not in the {registry.scope}::{registry.name} registry. ' # noqa: E501
f'Please check whether the value of `{obj_type}` is '
'correct or it was registered as expected. More details '
'can be found at '
Expand All @@ -117,12 +117,20 @@ def build_from_cfg(
else:
obj = obj_cls(**args) # type: ignore

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)
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}',
logger='current',
level=logging.DEBUG)
return obj


Expand Down
14 changes: 0 additions & 14 deletions mmengine/registry/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,6 @@ def _register_module(self,
raise TypeError(f'module must be Callable, but got {type(module)}')

if module_name is None:
assert hasattr(module, '__name__'), (
'If `name` is not provided for `register_module`, please make '
f'sure the {module} has the `__name__` attribute')
module_name = module.__name__
if isinstance(module_name, str):
module_name = [module_name]
Expand All @@ -613,17 +610,6 @@ def _register_module(self,
existed_module = self.module_dict[name]
raise KeyError(f'{name} is already registered in {self.name} '
f'at {existed_module.__module__}')
# If module is a partial function or user defined callable
# object, it may not have `__module__` and `__name__` attributes.
# We set the `__module__` and `__name__` attributes for them to
# provide more information.
if not hasattr(module, '__module__'):
module_name = str(inspect.getmodule(sys._getframe(2)))
if module_name is None:
module_name = '__main__'
module.__module__ = module_name
if not hasattr(module, '__name__'):
module.__name__ = name
self._module_dict[name] = module

def register_module(
Expand Down
10 changes: 0 additions & 10 deletions tests/test_registry/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,11 @@ class SphynxCat:
# lambda functions can be registered
CATS.register_module(name='unknown cat', module=lambda: 'unknown')

# name must be provided when module does not have `__name__` attribute.
with pytest.raises(AssertionError):
muchkin1 = functools.partial(muchkin, size=0)
CATS.register_module(module=muchkin1)

assert CATS.get('muchkin0') is muchkin0
assert 'unknown cat' in CATS
assert 'muchkin0' in CATS
assert len(CATS) == 11

# register duplicated name partial function
with pytest.raises(KeyError):
CATS.register_module('muchkin0', False, muchkin0)
CATS.register_module('muchkin0', True, muchkin0)

def _build_registry(self):
"""A helper function to build a Hierarchical Registry."""
# Hierarchical Registry
Expand Down

0 comments on commit 7a79153

Please sign in to comment.