Coverage for source/utils/singleton_meta.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-08-01 20:51 +0000

1# utils/singleton_meta.py 

2 

3# global imports 

4from typing import Type, Any 

5 

6# local imports 

7 

8class SingletonMeta(type): 

9 """ 

10 Metaclass for defining singleton classes. 

11 """ 

12 

13 def __new__(mcs, name: str, bases: tuple, attrs: dict) -> Type: 

14 """ 

15 Creates a new singleton class type. 

16 

17 Parameters: 

18 name (str): The name of the class being created. 

19 bases (tuple): The base classes of the class being created. 

20 attrs (dict): The attributes of the class being created. 

21 """ 

22 

23 cls = super().__new__(mcs, name, bases, attrs) 

24 cls.__singleton_instance = None 

25 return cls 

26 

27 def __call__(cls, *args, **kwargs) -> Any: 

28 """ 

29 Creates or returns the singleton instance of the class. 

30 

31 Parameters: 

32 (*args): Positional arguments for the class constructor 

33 (**kwargs): Keyword arguments for the class constructor 

34 

35 Returns: 

36 (Any): The singleton instance of the class. 

37 """ 

38 

39 if cls.__singleton_instance is None: 

40 cls.__singleton_instance = super().__call__(*args, **kwargs) 

41 return cls.__singleton_instance