Skip to content

bpo-24928: Add test case for patch.dict using OrderedDict #11437

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 5 commits into from
Jan 24, 2020

Conversation

eamanu
Copy link
Contributor

@eamanu eamanu commented Jan 5, 2019

bpo-24928: mock.patch.dict spoils order of items in collections.OrderedDict

This PR add test case for patch.dict using OrderedDict.

This is just test, don't need NEWS.

Co-authored-by: Yu Tomita nekobon@users.noreply.github.com

https://bugs.python.org/issue24928

@ZackerySpytz
Copy link
Contributor

Your PR is almost an exact copy of the Lib/unittest/test/testmock/testpatch.py changes
in https://bugs.python.org/file40488/issue24928.patch. That patch wasn't written
by you, so you should provide attribution to the original author, as stated in the developer's guide
(https://devguide.python.org/pullrequest/#converting-an-existing-patch-from-b-p-o-to-github).

original = foo.copy()
update_values = zip('cdefghijklmnopqrstuvwxyz', range(26))

@patch.dict(foo, OrderedDict(update_values))
Copy link
Member

@tirkarthi tirkarthi Jan 5, 2019

Choose a reason for hiding this comment

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

The assert looks incorrect that it's testing list(foo) == sorted(foo) but it should actually be testing whether foo contains update_values which on testing foo['c'] gives KeyError. Sorry, I just noticed it now since the tests passed with original patch.

I checked this and update_values is a zip that becomes empty once it's iterated over and can be converted to a list to make sure that update_values can be used for subsequent calls. So how about below to make sure order is preserved?

update_values = list(zip('cdefghijklmnopqrstuvwxyz', range(26)))
patched_items = list(foo.items()) + update_values

with patch.dict(foo, OrderedDict(update_values)):
    self.assertEqual(list(foo.items()), patched_items)

I would personally prefer a context manager here though other tests use @patch.dict with test() but that can wait for other reviewers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @tirkarthi

Yes I checked it manually and the foo['c'] gives the KeyError. I will change for the list(zip()) that you say. And I will use context manager, I think that is more readable.

Maybe, we could think about support convert zip to dict on OrderedDict()?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe, we could think about support convert zip to dict on OrderedDict()?

Sorry, are you saying to use patch.dict(foo, OrderedDict(dict(update_values))) ? dict guarantees order currently and would help in catching the error if it changes in future. So I would prefer having OrderedDict(update_values).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand now.

@eamanu
Copy link
Contributor Author

eamanu commented Jan 5, 2019

Your PR is almost an exact copy of the Lib/unittest/test/testmock/testpatch.py changes
in https://bugs.python.org/file40488/issue24928.patch. That patch wasn't written
by you, so you should provide attribution to the original author, as stated in the developer's guide
(https://devguide.python.org/pullrequest/#converting-an-existing-patch-from-b-p-o-to-github).

Thanks!. I added it to the PR description. In the next commit I will added it on commit message. Or is there other way to do it?

Co-authored-by: Yu Tomita nekobon@users.noreply.github.com
Copy link
Member

@tirkarthi tirkarthi left a comment

Choose a reason for hiding this comment

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

LGTM. I will wait for review from someone else. Thanks for the PR.

@csabella
Copy link
Contributor

@eamanu, please resolve the merge conflict and check the review by @mariocj89. Thanks!

@csabella
Copy link
Contributor

@eamanu, another reminder to please address the merge conflict and address the code review. If there isn't any activity on this issue, then it will need to be closed. Thank you!

@eamanu
Copy link
Contributor Author

eamanu commented Dec 14, 2019

@csabella so sorry I didn't see the messages. I will work on that today.

@eamanu
Copy link
Contributor Author

eamanu commented Dec 15, 2019

@csabella I believe this PR is ready for review

@csabella
Copy link
Contributor

csabella commented Jan 12, 2020

@eamanu, thanks for updating the PR!

@tirkarthi, would you like to re-review this and merge it? 🙂

@csabella csabella requested a review from tirkarthi January 12, 2020 12:47
@tirkarthi
Copy link
Member

I am fine with the test. @berkerpeksag had some opinions on the test in https://bugs.python.org/msg333890 so would wait for his approval on this.

@cjw296 cjw296 merged commit 1d0c5e1 into python:master Jan 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants