diff --git a/mmengine/utils/manager.py b/mmengine/utils/manager.py index 68f3409a6a..70b45f2d8e 100644 --- a/mmengine/utils/manager.py +++ b/mmengine/utils/manager.py @@ -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 @@ -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] diff --git a/tests/test_utils/test_manager.py b/tests/test_utils/test_manager.py index be9348e2d5..913affb649 100644 --- a/tests/test_utils/test_manager.py +++ b/tests/test_utils/test_manager.py @@ -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)