Coverage for /home/benjarobin/Bootlin/projects/Schneider-Electric-Senux/sbom-cve-check/src/sbom_cve_check/utils/class_utils.py: 90%

10 statements  

« prev     ^ index     » next       coverage.py v7.11.1, created at 2025-11-28 15:37 +0100

1# -*- coding: utf-8 -*- 

2# SPDX-License-Identifier: GPL-2.0-only 

3 

4from typing import Any, ClassVar, TypeVar 

5 

6_SingletonT = TypeVar("_SingletonT") 

7 

8 

9class Singleton(type): 

10 """ 

11 Implement the Singleton design pattern, restricting children classes to 

12 having a single instance. 

13 """ 

14 

15 __instances: ClassVar[dict[Any, "Singleton"]] = {} 

16 

17 def __call__(cls: type[_SingletonT], *args: Any, **kwargs: Any) -> _SingletonT: 

18 if not isinstance(cls, Singleton): 

19 raise TypeError("Not a Singleton metaclass") 

20 if cls not in cls.__instances: 

21 cls.__instances[cls] = super().__call__(*args, **kwargs) 

22 return cls.__instances[cls]