python / python/cpython

unittest.mock: Double period in AttributeError message for invalid assertion methods

未關閉
#137,716 6 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

When accessing a non-existent assertion method on a [Mock] object( e.g., assert_called_twice_with), the raised [AttributeError] message contains a double period (..) before the "Did you mean" suggestion.

Example

import unittest.mock as mock

m = mock.Mock()
m.assert_called_twice_with(1, 2)

Output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.12/unittest/mock.py", line 665, in __getattr__
    raise AttributeError(
AttributeError: 'assert_called_twice_with' is not a valid assertion. Use a spec for the mock if 'assert_called_twice_with' is meant to be an attribute.. Did you mean: 'assert_called_once_with'?

Problem
Note the double period ([attribute.. Did you mean] in the error message.

Expected Behavior
The error message should contain only a single period before the suggestion, e.g.:

Expected Output

AttributeError: 'assert_called_twice_with' is not a valid assertion. Use a spec for the mock if 'assert_called_twice_with' is meant to be an attribute. Did you mean: 'assert_called_once_with'?
CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PR
Linked PRs
  • gh-137717
  • gh-138111

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 unittest/mock.py 中的 Mock.getattr 開始,這是 traceback 中顯示的進入點。檢查現有的 unittest.mock 測試中針對無效 assertion 方法的部分,並調整訊息,使建議出現在一個句點之後;相關測試應確認修正後的 AttributeError 文字。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
testing-qa
Issue 類型
缺陷
難度
1/5
預估耗時
1-3 小時
活躍度
停滯
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。