重写方法中的签名不匹配¶
ID: py/inheritance/signature-mismatch
Kind: problem
Security severity:
Severity: warning
Precision: very-high
Tags:
- reliability
- correctness
Query suites:
- python-security-and-quality.qls
重写方法的一个(或多个)合法参数对于重写方法来说不合法。当重写方法被调用参数数量对于被重写方法来说是合法时,这将导致错误。这违反了里氏替换原则。
建议¶
确保重写方法接受所有对于被重写方法来说合法的参数。
示例¶
在这个例子中,基类方法 (self, source, filename, symbol)
和扩展方法 (self, source)
的合法参数之间存在不匹配。只要没有为 filename
和 symbol
参数指定值,扩展方法就可以用来重写基类方法。如果扩展方法被传递了基类方法接受的额外参数,那么就会发生错误。
# Base class method
def runsource(self, source, filename="<input>", symbol="single"):
... # Definition
# Extend base class method
def runsource(self, source):
... # Definition
应该更新扩展方法以支持被重写方法支持的 filename
和 symbol
参数。