Skip to main content
 首页 » 编程设计

python之如何让pylint识别模拟的方法成员

2024年11月01日22oomusou

测试示例:

import pytest 
def test_do_stuff(mocker): 
    import my_module 
    mocker.patch.object(my_module, 'do_stuff') 
    my_module.do_stuff.return_value = True 

Pylint 反馈:

E1101: Function 'do_stuff' has no 'return_value' member (no-member) 

这是不正确的,因为 my_module.do_stuff() 已被替换为支持此调用的模拟,但是,Pylint 似乎不理解这一点。

我可以使用测试文件顶部的 # pylint: disable=E1101 完全禁用非成员警告类型,但这也会隐藏合法的警告(例如,不正确的函数调用。)

有没有办法让 Pylint 与模拟对象一起工作?

请您参考如下方法:

据我所知,没有办法自动实现。

将 Pylint 禁用注释放在引发错误的行的末尾将仅抑制该行的警告并且不会停止对文件其余部分的分析,但注释将需要添加到每个有问题的行。