>>> from unittest import mock
... class Foo:
... bar = None
... patch = mock.patch.object(Foo, 'bar', 'x')
>>> patch.start()
'x'
>>> patch.start()
'x'
>>> patch.stop()
False
>>> patch.stop()
Traceback (most recent call last):
File "...", line ..., in runcode
coro = func()
File "<input>", line 1, in <module>
File "/usr/lib/python3.8/unittest/mock.py", line 1542, in stop
return self.__exit__(None, None, None)
File "/usr/lib/python3.8/unittest/mock.py", line 1508, in __exit__
if self.is_local and self.temp_original is not DEFAULT:
AttributeError: '_patch' object has no attribute 'is_local'
Bug report
It seems, such bug appeared after 4b222c9
Suppose we have initialized patch, than call
startmore than once and than callstopmore than once:But if we call
startonly once, multiplestops won't cause such error.For a first glance it is due to
stopexceptingValueErroron removing patch from_active_patchesand ifValueErrorwas not raised it proceeds assuming patch have attributeis_local.Your environment
Linked PRs