[Enhancement] add TypeVar type hint for ManagerMixin (#269)
parent
b01b3ff97c
commit
73da44805f
|
@ -2,9 +2,10 @@
|
||||||
import inspect
|
import inspect
|
||||||
import threading
|
import threading
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from typing import Any
|
from typing import Type, TypeVar
|
||||||
|
|
||||||
_lock = threading.RLock()
|
_lock = threading.RLock()
|
||||||
|
T = TypeVar('T')
|
||||||
|
|
||||||
|
|
||||||
def _accquire_lock() -> None:
|
def _accquire_lock() -> None:
|
||||||
|
@ -76,7 +77,7 @@ class ManagerMixin(metaclass=ManagerMeta):
|
||||||
self._instance_name = name
|
self._instance_name = name
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_instance(cls, name: str, **kwargs) -> Any:
|
def get_instance(cls: Type[T], name: str, **kwargs) -> T:
|
||||||
"""Get subclass instance by name if the name exists.
|
"""Get subclass instance by name if the name exists.
|
||||||
|
|
||||||
If corresponding name instance has not been created, ``get_instance``
|
If corresponding name instance has not been created, ``get_instance``
|
||||||
|
@ -102,11 +103,11 @@ class ManagerMixin(metaclass=ManagerMeta):
|
||||||
_accquire_lock()
|
_accquire_lock()
|
||||||
assert isinstance(name, str), \
|
assert isinstance(name, str), \
|
||||||
f'type of name should be str, but got {type(cls)}'
|
f'type of name should be str, but got {type(cls)}'
|
||||||
instance_dict = cls._instance_dict
|
instance_dict = cls._instance_dict # type: ignore
|
||||||
# Get the instance by name.
|
# Get the instance by name.
|
||||||
if name not in instance_dict:
|
if name not in instance_dict:
|
||||||
instance = cls(name=name, **kwargs)
|
instance = cls(name=name, **kwargs) # type: ignore
|
||||||
instance_dict[name] = instance
|
instance_dict[name] = instance # type: ignore
|
||||||
else:
|
else:
|
||||||
assert not kwargs, (
|
assert not kwargs, (
|
||||||
f'{cls} instance named of {name} has been created, the method '
|
f'{cls} instance named of {name} has been created, the method '
|
||||||
|
|
Loading…
Reference in New Issue