python / python/cpython

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

Open
#137,716 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in unittest/mock.py at Mock.getattr, the entry point shown in the traceback. Check the existing unittest.mock tests for invalid assertion methods and adjust the message so the suggestion follows a single period; the relevant tests should confirm the corrected AttributeError wording.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
1/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.