当我将类实例作为某些函数的参数传递时,Pylint无法检测到“类没有此类成员错误(例如:E1101)”
class HelloClass:
def __init__(self):
self.__message: str = "hello"
def say_hello(self) -> None:
print(self.__message)
def run(hello_instance: HelloClass) -> None:
hello_instance.say_hello()
hello_instance.say_blah_blah() # Not detected as error by pylint
my_hello_instance: HelloClass = HelloClass()
my_hello_instance.say_blah_blah() # Detected as error by pylint
run(my_hello_instance) # Runtime error
在上面的示例代码中,Pylint忽略了run()中错误键入的类函数名称say_blah_blah()
它与python的动态语言功能有关吗?
我可以通过一些pylint配置强制检查吗?
答案 0 :(得分:0)
是的,pylint忽略了类型错误的类函数,因为在Python中,您可以在代码中的任何位置进行动态添加。
您可以使用MyPy(pip install mypy
)进行静态类型检查。在这种情况下,它将显示"HelloClass" has no attribute "say_blah_blah"
条消息。