python - 在单元测试中使用补丁模块时,自动完成 pycharm 不起作用

以下代码工作正常,这是模拟打印功能的非常简单的测试。问题是当我调用 mocked_print 并希望自动完成 mocked_print 时,assert_called_once_with 方法没有出现在编辑器的建议中。

Here's an image of the suggestions I get

环境:

  • Windows 10
  • PyCharm 社区版 2019.2.2
  • python 3.7.4.exe
import unittest
from unittest import mock

def print_something():
    print('Hello')

class TestFoo(unittest.TestCase):
    def test_print(self):
        with mock.patch('builtins.print') as mocked_print:
            print_something()
            mocked_print.assert_called_once_with("Hello")

最佳答案

您可以导航到 mock.patch 定义并看到它被类型化为没有 assert_called_once_with_patch 对象。

PyCharm 使用来自 typeshed 的 stub 对于 unittest 所以似乎应该修改相应的 stub 。

https://stackoverflow.com/questions/58032232/

相关文章:

r - ChordDiagram 切断长标签

php - 尽管使用了有效 key ,Gnupg 仍给出 "get_key failed"

php - 自定义集合操作和IRI转换问题

python - 在pygame中检测键盘输入时只有某些键起作用

python - 如何在 seaborn python 中组合两个 relplots?

gitlab - 任何人都可以发布具有外部子模块的 GitLab CI 的工作示例吗?

ios - 使用 `xcodebuild` 归档时 CompileSwift 正常 arm64

regex - 如何按名称访问捕获组?

jenkins - 如何在参数化的 Jenkins 管道项目中检索作业名称

matplotlib - plt.show() 后不显示绘图,未给出错误 'and only sho