0

I am trying to create an AbstractClass using both abc.ABCMeta and QObject as parents and cannot seems to make it work.

Here is the Base class init. I have Pyqt5 and python 2.7

pyqtWrapperType = type(QObject)

class ParamsHandler(abc.ABCMeta, pyqtWrapperType):
    def __init__(self, device_model, read_only=False):
        super(ParamsHandler, self).__init__()
        self.cmd_to_get_data = None
        self.device_model = device_model

class ConfigParamsHandler(ParamsHandler):
    def __init__(self, device_model):
        super(ConfigParamsHandler, self).__init__(device_model)
        self.cmd_to_get_data = Commands.CONFIG_PARAMS

I get a TypeError('new() takes exactly 4 arguments (2 given)',) I also have Pycharm suggesting that I use cls instead of self

If I supply 4 arguments, it asks for a string as the first argument.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
PBareil
  • 157
  • 6

2 Answers2

2

abc.ABCMeta is supposed to be used as a metaclass. So instead of extending it, try:

class ParamsHandler(pyqtWrapperType):
    __metaclass__ = abc.ABCMeta
Fred
  • 1,462
  • 8
  • 15
  • Of course! I changed that, but I still get a TypeError('type() takes 1 or 3 arguments',) . I instantiante the class `ConfigParamsHandler(device_model)` – PBareil Mar 20 '18 at 12:34
  • Edited the answer – Fred Mar 20 '18 at 14:54
  • 1
    @Fred. The type of QObject is certainly not `type`: it's actually `sip.wrappertype`. Your current code will raise a TypeError (due to a metaclass conflict). – ekhumoro Mar 20 '18 at 17:56
2

I solved it using this approach instead:

class ParamsHandler_Meta(type(QObject), type(abc.ABCMeta)):
    pass
class ParamsHandlerClass(QObject):
    pass
class ParamsHandler(ParamsHandlerClass):
    __metaclass__ = ParamsHandler_Meta
    def __init__(self, device_model, cmd_to_get_data, read_only=False):
        super(ParamsHandler, self).__init__()
        self.cmd_to_get_data = cmd_to_get_data
PBareil
  • 157
  • 6