From a07123d1655bdc20235ac548ddb9e5ed62a8cc4a Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Mon, 12 Jan 2026 23:34:34 -0600 Subject: [PATCH] fix singleton typeing and __init__ --- src/libs/singleton.py | 7 +++++-- src/libs/singleton_raised.py | 11 +++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/libs/singleton.py b/src/libs/singleton.py index 5f791c0..1590162 100644 --- a/src/libs/singleton.py +++ b/src/libs/singleton.py @@ -1,4 +1,5 @@ # Python imports +from typing import Type, TypeVar, Any # Lib imports @@ -11,10 +12,12 @@ class SingletonError(Exception): +T = TypeVar('T', bound='Singleton') + class Singleton: _instance = None - def __new__(cls, *args, **kwargs): + def __new__(cls: Type[T], *args: Any, **kwargs: Any) -> T: if cls._instance is not None: logger.debug(f"'{cls.__name__}' is a Singleton. Returning instance...") return cls._instance @@ -22,7 +25,7 @@ class Singleton: cls._instance = super(Singleton, cls).__new__(cls) return cls._instance - def __init__(self): + def __init__(self) -> None: if self._instance is not None: return diff --git a/src/libs/singleton_raised.py b/src/libs/singleton_raised.py index e720553..c17b077 100644 --- a/src/libs/singleton_raised.py +++ b/src/libs/singleton_raised.py @@ -1,4 +1,5 @@ # Python imports +from typing import Type, TypeVar, Any # Lib imports @@ -11,16 +12,18 @@ class SingletonError(Exception): +T = TypeVar('T', bound='SingletonRaised') + class SingletonRaised: _instance = None - def __new__(cls, *args, **kwargs): + def __new__(cls: Type[T], *args: Any, **kwargs: Any) -> T: if cls._instance is not None: raise SingletonError(f"'{cls.__name__}' is a Singleton. Cannot create a new instance...") - cls._instance = super(Singleton, cls).__new__(cls) + cls._instance = super(SingletonRaised, cls).__new__(cls) return cls._instance - def __init__(self): - if cls._instance is not None: + def __init__(self) -> None: + if self._instance is not None: return