summaryrefslogtreecommitdiffstats
path: root/Lib/unittest
diff options
context:
space:
mode:
authorMichael Foord <voidspace@users.noreply.github.com>2019-09-13 16:40:56 (GMT)
committerStéphane Wirtel <stephane@wirtel.be>2019-09-13 16:40:56 (GMT)
commit14fd925a18fe3db0922a7d798e373102fe7a8a9c (patch)
tree620b59f67c3736c0efb5d905d5d05a075636e6c3 /Lib/unittest
parentf2b7556ef851ac85e7cbf189d1b29fdeb9539b88 (diff)
downloadcpython-14fd925a18fe3db0922a7d798e373102fe7a8a9c.zip
cpython-14fd925a18fe3db0922a7d798e373102fe7a8a9c.tar.gz
cpython-14fd925a18fe3db0922a7d798e373102fe7a8a9c.tar.bz2
bpo-38122: minor fixes to AsyncMock spec handling (GH-16099)
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/mock.py19
1 files changed, 8 insertions, 11 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 513cd5c..b33d58a 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -402,18 +402,12 @@ class NonCallableMock(Base):
# so we can create magic methods on the
# class without stomping on other mocks
bases = (cls,)
- if not issubclass(cls, AsyncMock):
+ if not issubclass(cls, AsyncMockMixin):
# Check if spec is an async object or function
- sig = inspect.signature(NonCallableMock.__init__)
- bound_args = sig.bind_partial(cls, *args, **kw).arguments
- spec_arg = [
- arg for arg in bound_args.keys()
- 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,)
+ bound_args = _MOCK_SIG.bind_partial(cls, *args, **kw).arguments
+ spec_arg = bound_args.get('spec_set', bound_args.get('spec'))
+ if spec_arg and _is_async_obj(spec_arg):
+ bases = (AsyncMockMixin, cls)
new = type(cls.__name__, bases, {'__doc__': cls.__doc__})
instance = object.__new__(new)
return instance
@@ -1020,6 +1014,9 @@ class NonCallableMock(Base):
return f"\n{prefix}: {safe_repr(self.mock_calls)}."
+_MOCK_SIG = inspect.signature(NonCallableMock.__init__)
+
+
class _AnyComparer(list):
"""A list which checks if it contains a call which may have an
argument of ANY, flipping the components of item and self from