pythonabcabstract-methodstorchserve

What the purpose of creating Python class inherited from `abc.ABC` but without `abstractmethod`?


I've read TorchServe's default handlers' sources and found that the BaseHandler is inherited from abc.ABC and doesn't have any abstract method. The VisionHandler is the same.

What could be the reason and when I should use abc.ABC without @abstractmethod?

PS: I found that one of the possible answers is to provide a common interface. In this case I think I still can use simple class without inheritance from ABC. No one would force you to implement all methods without abstractmethod decorator.


Solution

  • It signifies that the class is not meant to be called directly, but is meant to be extended. This is important when there's metaprogramming happening- if the class itself does stuff like review it's own properties or even its name as part of its processing.

    For PyTorch this is needed because the created handler class ends up as an entrypoint to the model, and gets included with the model archive files. Without extending the handler class you want to use pytorch wouldn't know which one to use. While they could have used a configuration file instead, the reality is most people do end up modifying their handlers so this is a cleaner way to handle it.