I'm new into python docstrings, so this question may be quite simple, however, I've not been able to find a clear response, probably because I'm not asking the right questions. I've found this question about docstring inheritance very usefull, however I need to make something a bit different: I want to modify the declared return type of a parent's function I don't need to override in son.
This is the simplified scenario: I've a pool of workers of different classes. All the workers share a common ancestor, let's name it BaseWorker
, and all the pools share a common ancestor too, BasePool
.
This is an example (VERY simplified) that allows me to use IDE's type hinting, however it involves an unecesary override of return_stored_worker():
class BaseWorker():
pass
class TypeAWorker(BaseWorker):
pass
class BasePool(object):
def __init__(self, n):
self._pool = {}
for i in range(n):
self._pool[i] = BasePool()
def return_stored_worker(self, id):
""" Blabla
Returns:
BaseWorker
"""
# Verifications and other stuff
return self._pool[id]
class TypeAPool(object):
def __init__(self, n):
self._pool = {}
for i in range(n):
self._pool[i] = TypeAWorker()
def return_stored_worker(self, id):
""" Blabla
Returns:
TypeAWorker
"""
return super().return_stored_worker(id)
As you can see, this does the trick, but introduces several nanoseconds to the calls that are completly non-sense from a programatic point of view.
When coded properly, TypeAPool
looks just like this:
class TypeAPool(object):
def __init__(self, n):
self._pool = {}
for i in range(n):
self._pool[i] = TypeAWorker()
My main purpose is to take advantage of IDE's autocomplete, rather than doc generation. Maybe there's a way to tell the docs that self._pool is actually an int -> TypeAWorker map, rather than just a dict, which is the real python class?