Skip to content

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

@rohanwinsor

Description

@rohanwinsor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions