-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
unittest mock's reset_mock throws an error when an attribute has been deleted #75360
Comments
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:
Gives:
Expected result: Only checked 3.5 and current master if bug is present |
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 Line 737 in 73820a6
Line 543 in 73820a6
➜ 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 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 |
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 |
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 (testing-cabal/mock#361), but my original PR there just avoided an exception without reinstating the deleted attribute. |
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. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: