I have a Python (3.8) metaclass for a singleton as seen here
I've tried to add typings like so:
from typing import Dict, Any, TypeVar, Type
_T = TypeVar("_T", bound="Singleton")
class Singleton(type):
_instances: Dict[Any, _T] = {}
def __call__(cls: Type[_T], *args: Any, **kwargs: Any) -> _T:
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]
In the line:
_instances: Dict[Any, _T] = {}
MyPy warns:
Mypy: Type variable "utils.singleton._T" is unbound
I've tried different iterations of this to no avail; it's very hard for me to figure out how to type this dict.
Further, the line:
def __call__(cls: Type[_T], *args: Any, **kwargs: Any) -> _T:
Produces:
Mypy: The erased type of self "Type[golf_ml.utils.singleton.Singleton]" is not a supertype of its class "golf_ml.utils.singleton.Singleton"
How could I correctly type this?