-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
Conversation
Your PR is almost an exact copy of the Lib/unittest/test/testmock/testpatch.py changes |
original = foo.copy() | ||
update_values = zip('cdefghijklmnopqrstuvwxyz', range(26)) | ||
|
||
@patch.dict(foo, OrderedDict(update_values)) |
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.
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.
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.
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()?
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.
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)
.
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.
I understand now.
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
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.
LGTM. I will wait for review from someone else. Thanks for the PR.
@eamanu, please resolve the merge conflict and check the review by @mariocj89. Thanks! |
@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! |
@csabella so sorry I didn't see the messages. I will work on that today. |
@csabella I believe this PR is ready for review |
@eamanu, thanks for updating the PR! @tirkarthi, would you like to re-review this and merge it? 🙂 |
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. |
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