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

Type hinting for generic singleton?

  • Thread starter Thread starter carbolymer
  • Start date Start date
C

carbolymer

Guest
I have a base service, which provides common methods for all services. Moreover, this BaseService serves as a service registry:

Code:
class BaseService:
    instances = {}

    @classmethod
    def get_instance(cls) -> 'BaseService':
        if cls.instances.get(cls) is None:
            cls.instances[cls] = cls()
        return cls.instances[cls]

class Service1(BaseService):
    pass

class Service2(BaseService):
    pass

Service1.get_instance()
Service2.get_instance()
Service1.get_instance()

The get_instance() method is returning the children class instance and I feel that the current annotation -> 'BaseService' is incorect. How should I annotate this method properly?
<p>I have a base service, which provides common methods for all services. Moreover, this <code>BaseService</code> serves as a service registry:</p>

<pre><code>class BaseService:
instances = {}

@classmethod
def get_instance(cls) -> 'BaseService':
if cls.instances.get(cls) is None:
cls.instances[cls] = cls()
return cls.instances[cls]

class Service1(BaseService):
pass

class Service2(BaseService):
pass

Service1.get_instance()
Service2.get_instance()
Service1.get_instance()
</code></pre>

<p>The <code>get_instance()</code> method is returning the children class instance and I feel that the current annotation <code>-> 'BaseService'</code> is incorect. How should I annotate this method properly?</p>
 

Online statistics

Members online
0
Guests online
3
Total visitors
3
Top