-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes #11057
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
Conversation
|
||
del mock.foo | ||
self.assertFalse(hasattr(mock, 'foo')) | ||
self.assertRaises(AttributeError, getattr, mock, 'foo') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems there is no test in the suite attempting to delete twice an attribute. That should raise, can you add one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 0c3dde0
@@ -0,0 +1,2 @@ | ||
Allow repeated assignment deletion of ``unittest.mock.Mock`` attributes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to link to the class here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 0c3dde0
if obj is _deleted: | ||
if name in self.__dict__: | ||
super().__delattr__(name) | ||
elif obj is _deleted: | ||
raise AttributeError(name) | ||
if obj is not _missing: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a comment in the patch about this line of checking _missing being superfluous and that it could be removed. Is there a test that will break on removing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, no test fail when removing this line. This does not mean is superfluous without inspecting. I prefer to handle that question in a different PR :)
Thanks for the catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this! A couple of tweaks and we're there...
self.assertRaises(AttributeError, getattr, mock, 'foo') | ||
|
||
|
||
def test_mock_raises_when_deleting_inexistent_attribute(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inexistent -> nonexistent, but what is this testing that isn't tested above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was requested by @mariocj89 because there is no test that tries to delete twice an attribute. The one above is checking that setting+deleting+setting+deleting does not raise, while this one checks that deleting twice does raise.
I will eliminate the extra checks now that we have a second test for that, so every test only test one thing.
@@ -1769,6 +1769,35 @@ def test_attribute_deletion(self): | ||
self.assertRaises(AttributeError, getattr, mock, 'f') | ||
|
||
|
||
def test_mock_does_not_raise_on_repeated_attribute_deletion(self): | ||
# bpo-20239: Assigning and deleting twice an attribute raises. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No sure we need the bpo comment, what will this mean in 10 years time when we're on a different tracker?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need it precisely to distinguish the tracker. For example, there is a PEP proposing moving to the GitHub issue tracker but the existing issues won't be moved. Therefore, issuexxxx is ambiguous while bpo-xxxx is not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant not having the comment at all, what value does the comment add? The test name and its code should be enough :-)
@@ -1769,6 +1769,35 @@ def test_attribute_deletion(self): | ||
self.assertRaises(AttributeError, getattr, mock, 'f') | ||
|
||
|
||
def test_mock_does_not_raise_on_repeated_attribute_deletion(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem like the right name, we appear to be testing that deleting an attribute of a mock does raise an AttributeError on the second attempt. test_delete_of_deleted_raises_attribute_error
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will eliminate the extra checks now that we have a second test for that.
NonCallableMock()): | ||
with self.assertRaises(AttributeError): | ||
del mock.foo | ||
del mock.foo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which of these two statements is doing the raising? Only one of them should be wrapped with the assertRaises
.
When you're done making the requested changes, leave the comment: |
I have made the requested changes; please review again |
Thanks for making the requested changes! @cjw296: please review the changes made to this pull request. |
@cjw296 Gentle ping |
@cjw296: Please replace |
Thanks @pablogsal for the PR, and @cjw296 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7. |
GH-11629 is a backport of this pull request to the 3.7 branch. |
…ythonGH-11057) * Allow repeated deletion of unittest.mock.Mock attributes * fixup! Allow repeated deletion of unittest.mock.Mock attributes * fixup! fixup! Allow repeated deletion of unittest.mock.Mock attributes (cherry picked from commit 222d303) Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
https://bugs.python.org/issue20239