From 38b22d9e685c820b8c4016e8f861d5b40ca35dcf Mon Sep 17 00:00:00 2001
From: jbwang1997 <jbwang1997@gmail.com>
Date: Tue, 31 May 2022 11:31:04 +0800
Subject: [PATCH] [Enhance] Enhance error report when a module has been
 registered in registery. (#264)

* Update

* Add unittest
---
 mmengine/registry/registry.py        | 5 +++--
 tests/test_registry/test_registry.py | 3 ++-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/mmengine/registry/registry.py b/mmengine/registry/registry.py
index aaac62ef..ac648b9e 100644
--- a/mmengine/registry/registry.py
+++ b/mmengine/registry/registry.py
@@ -480,8 +480,9 @@ class Registry:
             module_name = [module_name]
         for name in module_name:
             if not force and name in self._module_dict:
-                raise KeyError(f'{name} is already registered '
-                               f'in {self.name}')
+                existed_module = self.module_dict[name]
+                raise KeyError(f'{name} is already registered in {self.name} '
+                               f'at {existed_module.__module__}')
             self._module_dict[name] = module_class
 
     def register_module(
diff --git a/tests/test_registry/test_registry.py b/tests/test_registry/test_registry.py
index 7abf3502..2cc2ec84 100644
--- a/tests/test_registry/test_registry.py
+++ b/tests/test_registry/test_registry.py
@@ -115,7 +115,8 @@ class TestRegistry:
         # force=False
         with pytest.raises(
                 KeyError,
-                match='BritishShorthair is already registered in cat'):
+                match='BritishShorthair is already registered in cat '
+                'at test_registry'):
 
             @CATS.register_module()
             class BritishShorthair:
-- 
GitLab