Mismatched parameters in overridden methodPYL-W0221
The parameters in the overridden method don't match the method defined in the parent class. There are three possible scenarios for this:
- The number of parameters are different in both methods
- One or more names of the parameters are different in both methods
- The order of parameters is different in both methods
Python will allow this, but if the overridden method is intended to be executed from external code, you may want to reconsider this. Overriding a method without ensuring that both methods accept the same name, number, and order of parameters has the potential to cause an error when the overriding method is called with parameters that is illegal for the overridden method. This violates the Liskov substitution principle.
Bad practice
class Base:
def summation(cycle, x, y, z):
log(cycle)
return x + y + z
class UneqalArgLen(Base):
def summation(cycle, x, y):
log(cycle)
return x + y
class RenamedArg(Base):
def summation(cycle, x, y, zee=None):
log(cycle)
return x + y
class unorderdArgs(Base):
def summation(x, cycle, y, z):
log(cycle)
return x + y + z
Recommended
There can be multiple approaches to address this inconsistency. The first approach is to re-think the method signature in the parent class itself if the child classes are expected to implement a variable number of arguments in their own implementation. If the exploitation here is a special case, this can be dealt with making it compatible with the parent's class method signature, like this:
class Base:
def summation(cycle, x, y, z):
log(cycle)
return x + y + z
class FixedUneqalArgLen(Base):
def summation(cycle, x, y, z=None):
log(cycle)
return x + y
class NoRenamedArg(Base):
def summation(cycle, x, y, _): # using `_` won't trigger the issue.
log(cycle)
return x + y
class NounorderdArgs(Base):
def summation(cycle, x, y, z): # No surprises here
log(cycle)
return x + y + z