TLDR;
I am using a @classmethod
as a constructor for my class, and I need to override it with a different signature for one specific child class that needs extra parameters. PyCharm gives a warning about overriding a method with different signature. I wonder whether it also applies to @classmethod
constructors.
I am using the IDE PyCharm for my python project and I have received the following warning regarding the overriding of a method in a class:
Signature of method [...] does not match signature of base method in class [...]
I understand this is related to the Liskov substitution principle, meaning objects of a parent class should always be replaceable by objects of a child class.
However, in my case I am overriding a @classmethod
which is used as a constructor, following some sort of factory pattern. A simplification of my code would be as follows:
class Parent:
def __init__(self, common, data):
self.common = common
self.data = data
@classmethod
def from_directory(cls, data_dir, common):
all_data = [load_data(data_file) for data_file in get_data_files(data_dir)]
return [cls(common, data) for data in all_data]
class ChildA(Parent):
def __init__(self, common, data, specific):
super().__init__(common, data)
self.specific = specific
@classmethod
def from_directory(cls, data_dir, common, specific):
all_data = [load_data(data_file) for data_file in get_data_files(data_dir)]
return [cls(common, data, specific) for data in all_data]
In this example, basically I have a parent class Parent
with some common attribute that all child classes will inherit, and some particular child class ChildA
which has an extra, subclass-specific attribute.
Since I am using the @classmethod
as a constructor, I assume the Liskov principle does not apply, just in the same way that the __init__()
method can be overridden with a different signature. However, the PyCharm warning has made me consider whether there is something I might have missed. I am not sure whether I am using the @classmethod
in a sensitive way.
My main question is then: Is PyCharm being overzealous with its warnings here or is there any reason the pattern described above should be avoided?
Also, any feedback about any other design issues / misconceptions I might have is most welcome.