This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: unittest mock's reset_mock throws an error when an attribute has been deleted
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Eli_B, cjw296, hmvp, mariocj89, michael.foord, miss-islington, vstinner, xtreak
Priority: normal Keywords: patch

Created on 2017-08-10 14:22 by hmvp, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9302 merged xtreak, 2018-09-14 10:46
PR 10807 closed Eli_B, 2018-11-30 00:57
PR 10836 closed Eli_B, 2018-11-30 20:57
PR 10842 merged miss-islington, 2018-12-01 10:05
PR 10843 merged miss-islington, 2018-12-01 10:05
Messages (8)
msg300090 - (view) Author: Hmvp (hmvp) Date: 2017-08-10 14:22
When using a mock and deleting a attribute reset_mock cannot be used anymore since it tries to call reset_mock on the _deleted sentinel value.

Reproduction path:
```
from unittest.mock import MagicMock
mock = MagicMock()
mock.a = 'test'
del mock.a
mock.reset_mock()
```

Gives:
```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/unittest/mock.py", line 544, in reset_mock
    child.reset_mock(visited)
AttributeError: '_SentinelObject' object has no attribute 'reset_mock'
```

Expected result:
mock is reset without throwing an exception and the 'a' attribute is no longer in a deleted state

Only checked 3.5 and current master if bug is present
msg325340 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-09-14 10:10
Can confirm this behavior on CPython master as well. It seems that when an attribute is deleted then a deleted flag is set for the attribute at https://github.com/python/cpython/blob/73820a60cc3c990abb351540ca27bf7689bce8ac/Lib/unittest/mock.py#L737 . But when reset_mock is called it doesn't check for the deleted flag at https://github.com/python/cpython/blob/73820a60cc3c990abb351540ca27bf7689bce8ac/Lib/unittest/mock.py#L543

➜  cpython git:(master) ./python.exe ../backups/bpo31177.py
Traceback (most recent call last):
  File "../backups/bpo31177.py", line 5, in <module>
    m.reset_mock()
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 546, in reset_mock
    child.reset_mock(visited)
AttributeError: '_SentinelObject' object has no attribute 'reset_mock'

A simple patch would be to skip the deleted as below but some of the code in mock module raise an AttributeError. I don't know the correct behavior here. But applying the below patch and running tests with `./python.exe Lib/unittest/test/` doesn't cause any test failure.

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index db1e642c00..700e2fb8b9 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -541,7 +541,7 @@ class NonCallableMock(Base):
             self._mock_side_effect = None
 
         for child in self._mock_children.values():
-            if isinstance(child, _SpecState):
+            if isinstance(child, _SpecState) or child is _deleted:
                 continue
             child.reset_mock(visited)
 
I will try to make a PR if it's ok.

Thanks
msg326322 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-09-25 05:35
Adding Michael for thoughts on the fix and desired behavior. Removing 3.5 since only security fixes are accepted and adding 3.8 which is also affected.

Thanks
msg330740 - (view) Author: Eli_B (Eli_B) * Date: 2018-11-30 00:52
I suggest that after reset_mock(), deleted attributes should be available again. In other words, their deletion is reset.

I'm opening a PR to this effect.

I reported this issue to testing-cabal's mock repo in May 2016 (https://github.com/testing-cabal/mock/pull/361), but my original PR there just avoided an exception without reinstating the deleted attribute.
msg330846 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-12-01 10:04
New changeset edeca92c84a3b08902ecdfe987cde00c7e617887 by Victor Stinner (Xtreak) in branch 'master':
bpo-31177: Skip deleted attributes while calling reset_mock (GH-9302)
https://github.com/python/cpython/commit/edeca92c84a3b08902ecdfe987cde00c7e617887
msg330847 - (view) Author: miss-islington (miss-islington) Date: 2018-12-01 10:16
New changeset c0566e0ff6c2dd1a8b814ecd65649605c090527b by Miss Islington (bot) in branch '3.6':
bpo-31177: Skip deleted attributes while calling reset_mock (GH-9302)
https://github.com/python/cpython/commit/c0566e0ff6c2dd1a8b814ecd65649605c090527b
msg330848 - (view) Author: miss-islington (miss-islington) Date: 2018-12-01 10:24
New changeset 422c1658b7d34fdc73c5fc895b135862103d1983 by Miss Islington (bot) in branch '3.7':
bpo-31177: Skip deleted attributes while calling reset_mock (GH-9302)
https://github.com/python/cpython/commit/422c1658b7d34fdc73c5fc895b135862103d1983
msg330857 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-12-01 17:58
I am closing this as fixed since all the PRs were merged. Feel free to reopen this if needed. Thanks @mariocj89 and @vstinner for the review.
History
Date User Action Args
2022-04-11 14:58:50adminsetgithub: 75360
2018-12-01 17:58:42xtreaksetstatus: open -> closed
resolution: fixed
messages: + msg330857

stage: patch review -> resolved
2018-12-01 10:24:52miss-islingtonsetmessages: + msg330848
2018-12-01 10:16:29miss-islingtonsetnosy: + miss-islington
messages: + msg330847
2018-12-01 10:05:12miss-islingtonsetpull_requests: + pull_request10078
2018-12-01 10:05:04miss-islingtonsetpull_requests: + pull_request10077
2018-12-01 10:04:06vstinnersetnosy: + vstinner
messages: + msg330846
2018-11-30 20:57:25Eli_Bsetpull_requests: + pull_request10074
2018-11-30 02:14:44xtreaksetnosy: + cjw296, mariocj89
2018-11-30 00:57:17Eli_Bsetpull_requests: + pull_request10053
2018-11-30 00:52:26Eli_Bsetnosy: + Eli_B
messages: + msg330740
2018-09-25 05:35:14xtreaksetnosy: + michael.foord

messages: + msg326322
versions: + Python 3.8, - Python 3.5
2018-09-14 10:46:40xtreaksetkeywords: + patch
stage: patch review
pull_requests: + pull_request8731
2018-09-14 10:10:37xtreaksetnosy: + xtreak
messages: + msg325340
2017-08-10 14:22:19hmvpcreate