python / python/cpython

Might mock.MagicMock be given a type parameter?

未关闭
#117,222 2 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

type-feature
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

Proposal:

In the case of

class MyClass:
  def method_that_exists_on_my_class(self):
    pass

my_mock_thing = mock.create_autospec(MyClass)
my_mock_thing.method_that_exists_on_my_class()
my_mock_thing.method_that_does_not_exist_on_my_class()
my_mock_thing.method_that_exists_on_my_class.assert_called_once()
my_mock_thing.method_that_does_not_exist_on_my_class.assert_called_once()

it would be nice if the type of my_mock_thing were mock.MagicMock[MyClass] (instead of "raw" mock.MagicMock) so that type-checkers could alert me that my call to my_mock_thing.method_that_does_not_exist_on_my_class is a flaw in my test.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

(I looked around among both open and closed issues and didn't see anything on this topic; apologies if I overlooked an already-ongoing discussion...)

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

该提案围绕 mock.MagicMock 和 mock.create_autospec 展开;首先定位这些入口点,并检查当前如何对 mock 进行类型标注。然后确定与 autospec 属性访问相关的测试。当类型检查器能够标记 autospecced mock 上不存在的方法,同时保留对有效方法的访问时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
testing-qa
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。