Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Merged
merged 3 commits into from
Jan 21, 2019

Conversation

pablogsal
Copy link
Member

@pablogsal pablogsal commented Dec 9, 2018


del mock.foo
self.assertFalse(hasattr(mock, 'foo'))
self.assertRaises(AttributeError, getattr, mock, 'foo')
Copy link
Contributor

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?

Copy link
Member Author

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.
Copy link
Contributor

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.

Copy link
Member Author

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:
Copy link
Member

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?

Copy link
Member Author

@pablogsal pablogsal Dec 10, 2018

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!

Copy link
Contributor

@cjw296 cjw296 left a 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):
Copy link
Contributor

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?

Copy link
Member Author

@pablogsal pablogsal Dec 11, 2018

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.
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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):
Copy link
Contributor

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?

Copy link
Member Author

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
Copy link
Contributor

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.

@bedevere-bot
Copy link

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@pablogsal
Copy link
Member Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@cjw296: please review the changes made to this pull request.

@pablogsal
Copy link
Member Author

@cjw296 Gentle ping

@cjw296 cjw296 merged commit 222d303 into python:master Jan 21, 2019
@bedevere-bot
Copy link

@cjw296: Please replace # with GH- in the commit message next time. Thanks!

@miss-islington
Copy link
Contributor

Thanks @pablogsal for the PR, and @cjw296 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-11629 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 21, 2019
…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>
@pablogsal pablogsal deleted the bpo20239 branch May 19, 2021 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.