hayden0729
hayden0729

Reputation: 43

Does overriding an instance method with a class method violate LSP?

I have a class structure that looks something like this:

class A():

    @abstractmethod
    def getSize(self):
        return int()
    
class B(A):

    def __init__(self):
        self.size = 1

    def getSize(self):
        return self.size

class C(A):

    size = 2

    @classmethod
    def getSize(cls):
        return cls.size

The real calculation in getSize() is more than just storing a variable, but the point is that for B, it varies between instances, while for C, it's the same every time. Because of this, it'd be nice if I could make getSize() a class method for C, which would let me do C.getSize() rather than needing an instance. However, if I make it a class method like this, pylint complains with an arguments-differ message, which makes me think it might be a flawed approach. You can still use the method on an instance (i.e., C().getSize()), so I'm not really sure what the issue is. Is there a violation of LSP here? Is there a better way I could accomplish this, or should I just ignore the warning?

Upvotes: 1

Views: 174

Answers (0)

Related Questions