-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
Patcher stop method should be idempotent #80547
Comments
Currently, it's an error to call the stop() method of a patcher object created by mock.patch repeatedly: >>> patch = mock.patch.object(Foo, 'BAR', 'x')
>>> patch.start()
'x'
>>> patch.stop()
>>> patch.stop()
RuntimeError: stop called on unstarted patcher This causes unnecessary problems when test classes using mock.patch.stopall for cleanup are mixed with those cleaning up patches individually: class TestA(unittest.TestCase):
def setUp():
patcher = mock.patch.object(...)
self.mock_a = patcher.start()
self.addCleanup(patcher.stop) ... class TestB(TestA):
def setUp():
super().setUp()
self.addCleanup(mock.patch.stopall)
self.mock_b = mock.patch.object(...).start() ... This fails because mock.patch.stopall stops the patch set up in TestA.setUp(), then that raises an exception when it's stopped again. But why does patcher.stop() enforce that precondition? Wouldn't it be sufficient for it to just enforce the postcondition, that after stop() is called the patch is stopped()? That would make it easier to write test code which makes use of mock.patch.stopall, which allows the proper cleanup of patches to be configured much more concisely. |
Thank you |
When mock.patch is creates a patch object and patch.start calls __enter__ that sets is_local. On stop __exit__ is called where a check is done is to make sure is_local attribute is present and then cleanup is done along with deleting calling del self.is_local so calling stop second time causes the attribute check to fail. There is no specific reason I could find with git history. It seems that calling patch.stop without patch.start makes cleanup to happen on unpatched objects and raises errors which I hope is avoided by always setting is_local on start and removing it on stop like a flag. That being said I am not sure why a early return couldn't be made when is_local is absent instead of proceeding with cleanup logic or raising a runtime error. I see no tests failing on early return except a test where RuntimeError is intentionally tested by calling stop on unstarted patch. I have added mock module devs for some context. A sample script would be as below : from unittest import mock
class Foo:
bar = None
patch = mock.patch.object(Foo, 'bar', 'x')
patch.start()
patch.stop()
patch.stop() |
It's almost certainly an oversight rather than a design decision. I'd be happy with the change you suggest Karthikeyan. |
Thanks for the confirmation. I will raise a PR for this. |
Thanks, everyone! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: