-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
bpo-32299: unittest.mock.patch.dict now returns patched dict when used as context manager #4834
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
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Unfortunately our records indicate you have not signed the CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. We also demand... A SHRUBBERY! Thanks again to your contribution and we look forward to looking at it! |
Signed CLA, waiting my profile to be updated. |
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.
Looks good but NEWS entry and documentation update are required for the PR
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
up, CLA signed |
Docs update is required (https://docs.python.org/3.7/library/unittest.mock.html#patch-dict) |
will update it in a hour |
@asvetlov This is good to go. |
Documentation should be updated to reflect a value returned by context manager. |
…into fix-issue-32299
omg, forgot to push commit) sorry) |
Please add something like
at very end of |
done |
@@ -1331,6 +1332,8 @@ magic methods :meth:`__getitem__`, :meth:`__setitem__`, :meth:`__delitem__` and | |||
>>> assert thing['one'] == 1 | |||
>>> assert list(thing) == ['one'] | |||
|
|||
:func:`patch.dict` returns a patched dictionary when used as context manager. |
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.
This needs to be moved after the paragraph that starts with ":func:`patch.dict` can be used as a context manager, [...]".
Also, add a versionchanged marker after that paragraph:
.. versionchanged:: 3.7
:func:`patch.dict` now returns the patched dictionary when it used as a context manager.
(Feel free to use any wording you want, I just gave an example.)
def test_patch_dict_as_context_manager(self): | ||
foo = {'a': 'b'} | ||
with patch.dict(foo, a='c') as patched: | ||
self.assertDictEqual(patched, {'a': 'c'}) |
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 need to use assertDictEqual
explicitly here. assertEqual
will use assertDictEqual
if a
and b
both dicts:
Line 423 in 83c8675
self.addTypeEqualityFunc(dict, 'assertDictEqual') |
@@ -0,0 +1 @@ | |||
Changed ``mock.patch.dict`` to return patched dictionary when used as context manager |
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.
You way want to add 'the' before 'patched'.
@@ -0,0 +1 @@ | |||
Changed ``mock.patch.dict`` to return patched dictionary when used as context manager |
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.
Nitpick: Please finish the sentence with a full stop.
@@ -0,0 +1 @@ | |||
Changed ``mock.patch.dict`` to return patched dictionary when used as context manager |
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.
Please add "Patch by Your Name.".
... assert foo == {'newkey': 'newvalue'} | ||
... assert patched_foo == {'newkey': 'newvalue'} | ||
... |
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.
Could you also add the assignment example you gave in the issue?
patched_foo['spam'] = 'eggs'
assert patched_foo['spam'] == 'eggs'
@ts-taiye It looks like your patch is really close to being finished. If you include the last suggestions by @berkerpeksag , then they may be able to approve this. |
@ts-taiye this would be neat! Many people ask about why Do you plan on taking the changes? I'd be happy to help you out through the remaining bits if you want :) |
Yes, i plan to, try to do it tomorrow |
Sorry for misleading, still have no time, will really appreciate your help |
I think I've addressed all comments in mariocj89@ff1fbe7 Let me know if you want me to send a new PR which closes this one (you still get all the credit in the commit history 😉 ) or whether you want to apply the patch locally and update this PR. I've rebased it to remove the merge commit that was in this PR. If you want to apply locally, just get a new version of master and apply this patch. We can then ask a core dev to review it 😄 |
Hi @ts-taiye, this change would be neat! Every time I use As said, let me know if you don't have time to push the changes you asked and I put in mariocj89/cpython@ff1fbe7 and I will send a different PR (still with the commits and patch in your name). |
example:
https://bugs.python.org/issue32299