Skip to content

bpo-31177: Skip deleted attributes while calling reset_mock#9302

Merged
vstinner merged 7 commits into
python:masterfrom
tirkarthi:bpo31177
Dec 1, 2018
Merged

bpo-31177: Skip deleted attributes while calling reset_mock#9302
vstinner merged 7 commits into
python:masterfrom
tirkarthi:bpo31177

Conversation

@tirkarthi

@tirkarthi tirkarthi commented Sep 14, 2018

Copy link
Copy Markdown
Member

Skip the deleted attributes while calling reset_mock so that it doesn't cause an AttributeError .

Thanks

https://bugs.python.org/issue31177

Comment thread Lib/unittest/test/testmock/testmock.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest rewording this to something around:

"Fix bug that prevented using reset_mock on mock instances with deleted attributes"

@tirkarthi

Copy link
Copy Markdown
Member Author

Thanks @mariocj89 for the review. I have made the suggested changes.

@mariocj89 mariocj89 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thanks a lot :)

@vstinner, this seems to fix a bug in unittest.mock. When attributes are deleted in a mock they are marked with a mock.sentinel to prevent automatically creating other mocks dynamically, this is done by adding the _deleted sentinel to the child mocks dictionary.
The issue is that _reset_mock iterates over the list of child mocks assuming they are all Mocks without checking for the sentinel _deleted.

Comment thread Lib/unittest/test/testmock/testmock.py
@tirkarthi

tirkarthi commented Oct 27, 2018

Copy link
Copy Markdown
Member Author

@vstinner I have added the below comment to the test. Feel free to suggest rewording if needed.

[bpo-31177](https://bugs.python.org/issue31177): reset_mock should not raise AttributeError when attributes 
were deleted in a mock instance

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@tirkarthi

Copy link
Copy Markdown
Member Author

Please see #10807 that proposes deleted attributes to be retained that is different from my PR's behavior.

Comment thread Lib/unittest/test/testmock/testmock.py
@vstinner

Copy link
Copy Markdown
Member

Oops, I had a draft comment that I forgot to send!

@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @tirkarthi for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington

Copy link
Copy Markdown
Contributor

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

@bedevere-bot

Copy link
Copy Markdown

GH-10842 is a backport of this pull request to the 3.6 branch.

@bedevere-bot

Copy link
Copy Markdown

GH-10843 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 Dec 1, 2018
…-9302)

(cherry picked from commit edeca92)

Co-authored-by: Xtreak <tirkarthi@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 1, 2018
…-9302)

(cherry picked from commit edeca92)

Co-authored-by: Xtreak <tirkarthi@users.noreply.github.com>
miss-islington added a commit that referenced this pull request Dec 1, 2018
(cherry picked from commit edeca92)

Co-authored-by: Xtreak <tirkarthi@users.noreply.github.com>
miss-islington added a commit that referenced this pull request Dec 1, 2018
(cherry picked from commit edeca92)

Co-authored-by: Xtreak <tirkarthi@users.noreply.github.com>
@vstinner

vstinner commented Dec 1, 2018 via email

Copy link
Copy Markdown
Member

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.

6 participants