-
-
Notifications
You must be signed in to change notification settings - Fork 35k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1769,6 +1769,33 @@ def test_attribute_deletion(self): | |
| self.assertRaises(AttributeError, getattr, mock, 'f') | ||
|
|
||
|
|
||
| def test_mock_does_not_raise_on_repeated_attribute_deletion(self): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| # bpo-20239: Assigning and deleting twice an attribute raises. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :-) |
||
| for mock in (Mock(), MagicMock(), NonCallableMagicMock(), | ||
| NonCallableMock()): | ||
| mock.foo = 3 | ||
| self.assertTrue(hasattr(mock, 'foo')) | ||
| self.assertEqual(mock.foo, 3) | ||
|
|
||
| del mock.foo | ||
| self.assertFalse(hasattr(mock, 'foo')) | ||
|
|
||
| mock.foo = 4 | ||
| self.assertTrue(hasattr(mock, 'foo')) | ||
| self.assertEqual(mock.foo, 4) | ||
|
|
||
| del mock.foo | ||
| self.assertFalse(hasattr(mock, 'foo')) | ||
|
|
||
|
|
||
| def test_mock_raises_when_deleting_nonexistent_attribute(self): | ||
| for mock in (Mock(), MagicMock(), NonCallableMagicMock(), | ||
| NonCallableMock()): | ||
| del mock.foo | ||
| with self.assertRaises(AttributeError): | ||
| del mock.foo | ||
|
|
||
|
|
||
| def test_reset_mock_does_not_raise_on_attr_deletion(self): | ||
| # bpo-31177: reset_mock should not raise AttributeError when attributes | ||
| # were deleted in a mock instance | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Allow repeated assignment deletion of :class:`unittest.mock.Mock` attributes. | ||
| Patch by Pablo Galindo. |
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?
Uh oh!
There was an error while loading. Please reload this page.
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!