python / python/cpython

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

オープン
#137,716 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

トレースバックに示されているエントリポイントである Mock.getattr の unittest/mock.py から始めます。無効なアサーションメソッドに関する既存の unittest.mock テストを確認し、候補が 1 つのピリオドの後に続くようにメッセージを調整します。関連するテストでは、修正された AttributeError の文言を確認できるようにします。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
testing-qa
issue の種類
バグ
難易度
1/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。