Skip to content

Commit

Permalink
[Enhancement] Silence error when ManagerMixin built instance with d…
Browse files Browse the repository at this point in the history
…uplicate name. (#990)

* [Fix]Silence error when ManagerMixin built duplicate name instance

* [Fix]Silence error when ManagerMixin built duplicate name instance

* Update mmengine/utils/manager.py

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

---------

Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
  • Loading branch information
HAOCHENYE and zhouzaida committed Mar 10, 2023
1 parent 8beacd3 commit 7a074fa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions mmengine/utils/manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) OpenMMLab. All rights reserved.
import inspect
import threading
import warnings
from collections import OrderedDict
from typing import Type, TypeVar

Expand Down Expand Up @@ -108,10 +109,11 @@ def get_instance(cls: Type[T], name: str, **kwargs) -> T:
if name not in instance_dict:
instance = cls(name=name, **kwargs) # type: ignore
instance_dict[name] = instance # type: ignore
else:
assert not kwargs, (
f'{cls} instance named of {name} has been created, the method '
'`get_instance` should not access any other arguments')
elif kwargs:
warnings.warn(
f'{cls} instance named of {name} has been created, '
'the method `get_instance` should not accept any other '
'arguments')
# Get latest instantiated instance or root instance.
_release_lock()
return instance_dict[name]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ def test_get_instance(self):
SubClassA.get_instance(name=1)
# `get_instance` should not accept other arguments if corresponding
# instance has been created.
with pytest.raises(AssertionError):
with pytest.warns(UserWarning):
SubClassA.get_instance('name2', a=1, b=2)

0 comments on commit 7a074fa

Please sign in to comment.