0

I would like to create an interface on top of a QAbstractTableModel. To test so, I derived the following snippet which reproduces my problem:

import abc
from PyQt5 import QtCore

class IFileSystemModel(QtCore.QAbstractTableModel):
    
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def toto(self):
        pass

class LocalFileSystemModel(IFileSystemModel):
    pass


l = LocalFileSystemModel()

Running that code, I would expect the following exception TypeError: Can't instantiate abstract class LocalFileSystemModel with abstract methods toto. Instead, the code does not throw. I may misunderstand something at this stage. Could you point me to the right direction please ?

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
Eurydice
  • 8,001
  • 4
  • 24
  • 37
  • 1
    `__metaclass__` doesn't exist in python3 (see [here](https://stackoverflow.com/q/7196376/984421)). Also, it makes no sense to declare the abstract method on the derived class; it should be part of the interface. But even if you fix these basic issues, you're going run into lots of problems getting this to work properly with PyQt/PySide. In the long run, I very much doubt that it will be worth all the hassle. – ekhumoro Aug 05 '21 at 11:52
  • Possible duplicate of https://stackoverflow.com/q/28799089/984421. The solution given in [this answer](https://stackoverflow.com/a/64750313/984421) seems to work, but I did not do any thorough testing. – ekhumoro Aug 05 '21 at 12:08

1 Answers1

2

Based on this post, here is a possible solution.

import abc
from PyQt5 import QtCore

class MyMeta(abc.ABCMeta, type(QtCore.QAbstractTableModel)):
    pass

class IFileSystemModel(QtCore.QAbstractTableModel, metaclass=MyMeta):
    
    @abc.abstractmethod
    def toto(self):
        pass

class LocalFileSystemModel(IFileSystemModel):
    pass

l = LocalFileSystemModel()

In such case, python will actually throws about a missing implementation of toto method.

Eurydice
  • 8,001
  • 4
  • 24
  • 37