OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python singleton pattern with type hints

  • Thread starter Thread starter Vikash Balasubramanian
  • Start date Start date
V

Vikash Balasubramanian

Guest
I have been trying to use the Singleton pattern in Python with proper type hints.

Here is my attempt

Code:
from typing import Any, TypeVar, override


T = TypeVar('T', bound='SingletonBase')


class Singleton(type):
    _instances: dict[type[T], T] = {}  # type variable T has no meaning in this context

    @override
    def __call__(cls: type[T], *args: Any, **kwargs: dict[str, Any]) -> T:
        if cls not in cls._instances:  # Access to generic instance variable through class is ambiguous
            instance = super().__call__(*args, **kwargs)
            cls._instances[cls] = instance # Access to generic instance variable through class is ambiguous
        return cls._instances[cls]  # Expression of type T is incompatible with return type T@__call__


class SingletonBase(metaclass=Singleton):
    pass

I get a lot of complaints from the type checker. (I have annotate my code with the complaints as comments)

I found this answer Type hinting for generic singleton?

But that only handles annotations for the return type. I would like to understand what's going on here and learn how to implement the Singleton pattern with proper type hints.
<p>I have been trying to use the Singleton pattern in Python with proper type hints.</p>
<p>Here is my attempt</p>
<pre class="lang-py prettyprint-override"><code>from typing import Any, TypeVar, override


T = TypeVar('T', bound='SingletonBase')


class Singleton(type):
_instances: dict[type[T], T] = {} # type variable T has no meaning in this context

@override
def __call__(cls: type[T], *args: Any, **kwargs: dict[str, Any]) -> T:
if cls not in cls._instances: # Access to generic instance variable through class is ambiguous
instance = super().__call__(*args, **kwargs)
cls._instances[cls] = instance # Access to generic instance variable through class is ambiguous
return cls._instances[cls] # Expression of type T is incompatible with return type T@__call__


class SingletonBase(metaclass=Singleton):
pass
</code></pre>
<p>I get a lot of complaints from the type checker. (I have annotate my code with the complaints as comments)</p>
<p>I found this answer <a href="https://stackoverflow.com/a/43024667/4126652">Type hinting for generic singleton?</a></p>
<p>But that only handles annotations for the return type. I would like to understand what's going on here and learn how to implement the Singleton pattern with proper type hints.</p>
 

Latest posts

A
Replies
0
Views
1
Alvah_Franey
A
H
Replies
0
Views
1
habrewning
H
Top