bpo-38122: minor fixes to AsyncMock spec handling (GH-16099)

This commit is contained in:
Michael Foord 2019-09-13 18:40:56 +02:00 committed by Stéphane Wirtel
parent f2b7556ef8
commit 14fd925a18
1 changed files with 8 additions and 11 deletions

View File

@ -402,18 +402,12 @@ class NonCallableMock(Base):
# so we can create magic methods on the # so we can create magic methods on the
# class without stomping on other mocks # class without stomping on other mocks
bases = (cls,) bases = (cls,)
if not issubclass(cls, AsyncMock): if not issubclass(cls, AsyncMockMixin):
# Check if spec is an async object or function # Check if spec is an async object or function
sig = inspect.signature(NonCallableMock.__init__) bound_args = _MOCK_SIG.bind_partial(cls, *args, **kw).arguments
bound_args = sig.bind_partial(cls, *args, **kw).arguments spec_arg = bound_args.get('spec_set', bound_args.get('spec'))
spec_arg = [ if spec_arg and _is_async_obj(spec_arg):
arg for arg in bound_args.keys() bases = (AsyncMockMixin, cls)
if arg.startswith('spec')
]
if spec_arg:
# what if spec_set is different than spec?
if _is_async_obj(bound_args[spec_arg[0]]):
bases = (AsyncMockMixin, cls,)
new = type(cls.__name__, bases, {'__doc__': cls.__doc__}) new = type(cls.__name__, bases, {'__doc__': cls.__doc__})
instance = object.__new__(new) instance = object.__new__(new)
return instance return instance
@ -1020,6 +1014,9 @@ class NonCallableMock(Base):
return f"\n{prefix}: {safe_repr(self.mock_calls)}." return f"\n{prefix}: {safe_repr(self.mock_calls)}."
_MOCK_SIG = inspect.signature(NonCallableMock.__init__)
class _AnyComparer(list): class _AnyComparer(list):
"""A list which checks if it contains a call which may have an """A list which checks if it contains a call which may have an
argument of ANY, flipping the components of item and self from argument of ANY, flipping the components of item and self from