1

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?

DGoiko
  • 346
  • 2
  • 19
  • And what IDE are you using? Modern IDEs support Python's type hining. – Martijn Pieters Jan 11 '19 at 16:56
  • @MartijnPieters I'm using PyCharm. However, keep in mind that I DO NOT WANT to override return_stored_worker in TypeAPool, so I'd need to do the type hinting in self._pool. I found out that you should be able to tell it :type: dict of (int, TypeAWorker), but it is not properly hinting :S, probably because return_stored_worker is not as simple as the one in the example and doesn't have self._pool[id] in the return: it performs several checks before and returns a copied reference to the object, so the IDE misses the hint. – DGoiko Jan 11 '19 at 17:56
  • That's what [generics](https://mypy.readthedocs.io/en/latest/generics.html) are *for*. You'd define your base pool as a generic class and your `TypeAPool` as a specific *instance*, where the `TypeAWorker` fills the generic slot. Via an argument to `__init__`, for example. – Martijn Pieters Jan 11 '19 at 17:58
  • @MartijnPieters will try setting self._pool as a generic dict, but I'm afraid that the IDE may not be smart enough to catch up in the autocomplete. I don't use'em since the BasePool class is the one that creates every worker, so unless someone access _pool without permission, it should never be a problem. I know private scope of _ is just a convention, but seem'd safe enough for a quick prototype. – DGoiko Jan 11 '19 at 18:13
  • Please do check how Generics work. You annotate the return value of the methods, and the type checker knows that when you create an instance with `TypeAWorker` as an argument, that methods will return instances of that class. Autocompletion will pick that up. – Martijn Pieters Jan 11 '19 at 18:15

0 Answers0