Skip to content

Conversation

ts-taiye
Copy link

@ts-taiye ts-taiye commented Dec 13, 2017

example:

>>> from unittest.mock import patch
>>> with patch.dict(a, a=2) as patched:
...     print(patched)
... 
{'a': 2}
>>> print(a)
{'a': '1'}

https://bugs.python.org/issue32299

@the-knights-who-say-ni
Copy link

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!

@ts-taiye
Copy link
Author

Signed CLA, waiting my profile to be updated.
Should i add something to news?

Copy link
Contributor

@asvetlov asvetlov left a 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

@bedevere-bot
Copy link

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@ts-taiye
Copy link
Author

ts-taiye commented Dec 14, 2017

up, CLA signed

@asvetlov
Copy link
Contributor

Docs update is required (https://docs.python.org/3.7/library/unittest.mock.html#patch-dict)
Source file is ./Docs/library/unittest-mock.rst

@asvetlov asvetlov self-assigned this Dec 14, 2017
@ts-taiye
Copy link
Author

will update it in a hour

@srinivasreddy
Copy link
Contributor

@asvetlov This is good to go.

@asvetlov
Copy link
Contributor

Documentation should be updated to reflect a value returned by context manager.

@ts-taiye
Copy link
Author

omg, forgot to push commit) sorry)

@asvetlov
Copy link
Contributor

Please add something like

      :func:`patch.dict` returns a patched dictionary when used as context manager.

at very end of patch.dict chapter, just before next patch.multiple one.

@ts-taiye
Copy link
Author

ts-taiye commented Jan 4, 2018

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

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'})
Copy link
Member

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:

self.addTypeEqualityFunc(dict, 'assertDictEqual')

@@ -0,0 +1 @@
Changed ``mock.patch.dict`` to return patched dictionary when used as context manager
Copy link
Member

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

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

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'}
...
Copy link
Member

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'

@csabella
Copy link
Contributor

@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.

@mariocj89
Copy link
Contributor

@ts-taiye this would be neat! Many people ask about why mock.patch.dict does not return the patched object like mock.patch.object or mock.patch. It makes it not so intuitive

Do you plan on taking the changes? I'd be happy to help you out through the remaining bits if you want :)

@ts-taiye
Copy link
Author

@ts-taiye this would be neat! Many people ask about why mock.patch.dict does not return the patched object like mock.patch.object or mock.patch. It makes it not so intuitive

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

@ts-taiye
Copy link
Author

ts-taiye commented Nov 5, 2018

@ts-taiye this would be neat! Many people ask about why mock.patch.dict does not return the patched object like mock.patch.object or mock.patch. It makes it not so intuitive

Do you plan on taking the changes? I'd be happy to help you out through the remaining bits if you want :)

Sorry for misleading, still have no time, will really appreciate your help

@mariocj89
Copy link
Contributor

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 😄

@mariocj89
Copy link
Contributor

Hi @ts-taiye, this change would be neat! Every time I use patch.dict I wish this was merged :)

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).

@mariocj89
Copy link
Contributor

@ts-taiye I've opened with your change: #11062

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.

9 participants