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
« 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
4from typing import Any, ClassVar, TypeVar
6_SingletonT = TypeVar("_SingletonT")
9class Singleton(type):
10 """
11 Implement the Singleton design pattern, restricting children classes to
12 having a single instance.
13 """
15 __instances: ClassVar[dict[Any, "Singleton"]] = {}
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]