-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
reset_mock
resets MagicMock
's magic methods in an unexpected way
#123934
Comments
reset_mock
resets MagicMock
's magic methods in an unexpected way
Thank you for the report. I will take a look soon :) |
Yes, this is correct.
print(type(mock.Mock().__str__))
# <class 'method-wrapper'>
print(type(mock.MagicMock().__str__))
# <class 'unittest.mock.MagicMock'> But, I agree that retuning
|
…ues (pythonGH-124038) (cherry picked from commit 7628f67) Co-authored-by: sobolevn <mail@sobolevn.me>
…ues (pythonGH-124038) (cherry picked from commit 7628f67) Co-authored-by: sobolevn <mail@sobolevn.me>
Bug report
Bug description:
The
reset_mock(return_value=True)
method behaves in a wrong/inconsistent way.When used with
MagicMock
, the methodreset_mock(return_value=True)
does not reset the return values of the magic methods. Only if you call for example__str__
and then call the reset_mock function, the return value will be reset, but not to the default value.Output
Since Python 3.9 PR
reset_mock
now also resets child mocks. This explains the behaviour. Calling the__str__
method creates a childMagicMock
with a set return value. Since this child mock now exists, its return value is reset when reset_mock(return_value=True) is called.Although this can be logically explained, it's counter-intuitive and annoying as I'm never sure which values are being reset.
I would expect the same behaviour as
Mock
. The return value of__str__
and other magic methods should not be effected.Output
CPython versions tested on:
3.10
Operating systems tested on:
Linux
Linked PRs
MagicMock
not to reset magic method return values #124038MagicMock
not to reset magic method return values (GH-124038) #124231MagicMock
not to reset magic method return values (GH-124038) #124232The text was updated successfully, but these errors were encountered: