Skip to content

Fix double period typo in unittest.mock AttributeError message #137869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

AzizCode92
Copy link

Summary

Fix a typo in the unittest.mock error message where a double period (..) appeared in the AttributeError when an invalid assertion method is used.

Before:

>>> from unittest import mock
>>> m = mock.Mock()
>>> m(1,2)
<Mock name='mock()' id='4344084816'>
>>> m.assert_called_twice()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/Cellar/python@3.12/3.12.11/Frameworks/Python.framework/Versions/3.12/lib/python3.12/unittest/mock.py", line 665, in __getattr__
    raise AttributeError(
AttributeError: 'assert_called_twice' is not a valid assertion. Use a spec for the mock if 'assert_called_twice' is meant to be an attribute.. Did you mean: 'assert_called_once'?

After:

>>> from unittest import mock
>>> m = mock.Mock()
>>> m(1,2)
<Mock name='mock()' id='4345214112'>
>>> m.assert_called_twice()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/Cellar/python@3.12/3.12.11/Frameworks/Python.framework/Versions/3.12/lib/python3.12/unittest/mock.py", line 665, in __getattr__
    raise AttributeError(
AttributeError: 'assert_called_twice' is not a valid assertion. Use a spec for the mock if 'assert_called_twice' is meant to be an attribute. Did you mean: 'assert_called_once'?

This is a trivial typo fix that improves the user experience when debugging mock assertion errors.

@AzizCode92 AzizCode92 requested a review from cjw296 as a code owner August 16, 2025 21:45
@python-cla-bot
Copy link

python-cla-bot bot commented Aug 16, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Aug 16, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixz
Copy link
Member

picnixz commented Aug 16, 2025

Does it happen with AttributeError in general and suggestions? if so this warrants an issue and the fix can be at the suggestion level

@brianschubert
Copy link
Member

brianschubert commented Aug 16, 2025

We do not accept PRs for maintenance branches (and 3.12 is security only anyway). This was fixed in main by #137717

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants