File size: 446 Bytes
e35e6bc
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from __future__ import annotations
from typing import Any, ClassVar

SingletonMetaType = type["SingletonMeta"]


class SingletonMeta(type):
    _instances: ClassVar[dict[type, SingletonMetaType]] = {}

    def __call__(cls, *args: Any, **kwargs: Any) -> SingletonMetaType:
        if cls not in cls._instances:
            instance = super().__call__(*args, **kwargs)
            cls._instances[cls] = instance
        return cls._instances[cls]