简体   繁体   中英

Signature does not match base method in class

Please help understand how to specify the base class method signature which can take at least one and potentially additional arguments.

I need to have a forward method which takes at least X, and additional arguments which may not required depending on a sub class.

class Layer:
    def forward(self, X, *args) -> float:

The child class SoftmaxWithLogLoss forward has X and an additional argument T.

class SoftmaxWithLogLoss(Layer):
    def forward(self, X: np.ndarray, T: np.ndarray) -> float:  <--- Signature does not match 

It causes a warning Signature of method 'SoftmaxWithLogLoss.forward()' does not match signature of base method in class 'Layer' .

Please explain what the cause is and how to fix.

I guess it is mypy that raises the error. The reason is a violation of Liskov substitution principle https://en.wikipedia.org/wiki/Liskov_substitution_principle .

All base class objects must be replaceable with their subclass objects. However since the signature of the method in the subclass is different, then it cannot be done.

You can find more here Mypy produces incompatible signature error but Liskov Substitution Principle is satisfied

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM