bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes#11057
Conversation
|
|
||
| del mock.foo | ||
| self.assertFalse(hasattr(mock, 'foo')) | ||
| self.assertRaises(AttributeError, getattr, mock, 'foo') |
There was a problem hiding this comment.
Seems there is no test in the suite attempting to delete twice an attribute. That should raise, can you add one?
| @@ -0,0 +1,2 @@ | |||
| Allow repeated assignment deletion of ``unittest.mock.Mock`` attributes. | |||
There was a problem hiding this comment.
You might want to link to the class here.
| super().__delattr__(name) | ||
| elif obj is _deleted: | ||
| raise AttributeError(name) | ||
| if obj is not _missing: |
There was a problem hiding this comment.
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.
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!
cjw296
left a comment
There was a problem hiding this comment.
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.
inexistent -> nonexistent, but what is this testing that isn't tested above?
There was a problem hiding this comment.
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.
|
|
||
|
|
||
| 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.
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.
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.
I meant not having the comment at all, what value does the comment add? The test name and its code should be enough :-)
| self.assertRaises(AttributeError, getattr, mock, 'f') | ||
|
|
||
|
|
||
| def test_mock_does_not_raise_on_repeated_attribute_deletion(self): |
There was a problem hiding this comment.
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.
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.
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