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 摘要。