Skip to content

bpo-35512: Resolve string target to patch.dict decorator during function call#12000

Merged
cjw296 merged 5 commits into
python:masterfrom
tirkarthi:bpo35512
Feb 24, 2019
Merged

bpo-35512: Resolve string target to patch.dict decorator during function call#12000
cjw296 merged 5 commits into
python:masterfrom
tirkarthi:bpo35512

Conversation

@tirkarthi

@tirkarthi tirkarthi commented Feb 23, 2019

Copy link
Copy Markdown
Member

String target provided to patch.dict can be reassigned after the decorator decorates the function. So resolve the target before function call to ensure the new reference is patched instead of patching the old reference stored in the constructor.

https://bugs.python.org/issue35512

@tirkarthi

Copy link
Copy Markdown
Member Author

@mariocj89 I have just moved the resolution to _patch_dict since I felt storing the target in in_dict_name is little misleading since it's not always a name. Also that using a separate variable for only string target felt little verbose to me since it's essentially the same thing.

For tests I have used testmock.support to have a dictionary that I am trying to patch as a string target in testpatch.py which patches the older reference without the PR. Let me know if this could be made better and comments/NEWS need to be reworded.

Thanks

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

I think it is a little bit weird that self.in_dict changes when executing the function/ctx manager but given that we are using it just as a way to make it available across functions LGTM.

@tirkarthi

Copy link
Copy Markdown
Member Author

Thanks for the review Mario. I would wait for feedback from @jaraco and @cjw296 to see if this can be merged.

Comment thread Lib/unittest/test/testmock/testpatch.py Outdated
self.assertEqual(support.target['bar'], 'BAR')

support.target = {'foo': 'BAZ'}
test()

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 expect this test() call to be wrapped in a try/finally to put support.target back like it was, regardless of what happens in test.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, tests might use the variable in the future. Good catch. Thanks.

Comment thread Lib/unittest/test/testmock/testpatch.py Outdated
test()

self.assertEqual(support.target['foo'], 'BAZ')
self.assertNotIn('bar', support.target)

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.

Why not just:
self.assertEqual(support.target, {'foo': 'BAZ'})
?
(I guess the same goes for the assertions inside test too?)

Comment thread Lib/unittest/mock.py

def __init__(self, in_dict, values=(), clear=False, **kwargs):
if isinstance(in_dict, str):
in_dict = _importer(in_dict)

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.

Is the setting of self.in_dict on the line below still needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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

LGTM!


try:
support.target = {'foo': 'BAZ'}
test()

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.

You could always add the following here, so be super sure:
self.assertEqual(support.target, {'foo': 'BAZ'})

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, added with d7f5c11

@cjw296
cjw296 merged commit a875ea5 into python:master Feb 24, 2019
@miss-islington

Copy link
Copy Markdown
Contributor

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

@tirkarthi

Copy link
Copy Markdown
Member Author

Thanks @cjw296 for merging this :)

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