From db5e86adbce12350c26e7ffc2c6673369971a2dc Mon Sep 17 00:00:00 2001 From: Chris Withers Date: Wed, 29 Jan 2020 16:24:54 +0000 Subject: Get mock coverage back to 100% (GH-18228) * use the `: pass` and `: yield` patterns for code that isn't expected to ever be executed. * The _Call items passed to _AnyComparer are only ever of length two, so assert instead of if/else * fix typo * Fix bug, where stop-without-start patching dict blows up with `TypeError: 'NoneType' object is not iterable`, highlighted by lack of coverage of an except branch. * The fix for bpo-37972 means _Call.count and _Call.index are no longer needed. * add coverage for calling next() on a mock_open with readline.return_value set. * __aiter__ is defined on the Mock so the one on _AsyncIterator is never called. --- Lib/unittest/mock.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) (limited to 'Lib/unittest/mock.py') diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 37ea5c7e45..9e692981a2 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -1042,8 +1042,7 @@ class _AnyComparer(list): the left.""" def __contains__(self, item): for _call in self: - if len(item) != len(_call): - continue + assert len(item) == len(_call) if all([ expected == actual for expected, actual in zip(item, _call) @@ -1856,7 +1855,8 @@ class _patch_dict(object): def __exit__(self, *args): """Unpatch the dict.""" - self._unpatch_dict() + if self._original is not None: + self._unpatch_dict() return False @@ -2168,7 +2168,7 @@ class AsyncMockMixin(Base): self.__dict__['__code__'] = code_mock async def _execute_mock_call(self, /, *args, **kwargs): - # This is nearly just like super(), except for sepcial handling + # This is nearly just like super(), except for special handling # of coroutines _call = self.call_args @@ -2541,12 +2541,6 @@ class _Call(tuple): return tuple.__getattribute__(self, attr) - def count(self, /, *args, **kwargs): - return self.__getattr__('count')(*args, **kwargs) - - def index(self, /, *args, **kwargs): - return self.__getattr__('index')(*args, **kwargs) - def _get_call_arguments(self): if len(self) == 2: args, kwargs = self @@ -2917,9 +2911,6 @@ class _AsyncIterator: code_mock.co_flags = inspect.CO_ITERABLE_COROUTINE self.__dict__['__code__'] = code_mock - def __aiter__(self): - return self - async def __anext__(self): try: return next(self.iterator) -- cgit v1.2.1