summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/mock.py
diff options
context:
space:
mode:
authorLisa Roach <lisaroach14@gmail.com>2019-09-20 04:04:18 (GMT)
committerGitHub <noreply@github.com>2019-09-20 04:04:18 (GMT)
commit8b03f943c37e07fb2394acdcfacd066647f9b1fd (patch)
treec1e3951fe13dc337f99b78bd3521867dd19c2ac6 /Lib/unittest/mock.py
parent2702638eabe5f7b25f36d295f0ad78cb8d4eda05 (diff)
downloadcpython-8b03f943c37e07fb2394acdcfacd066647f9b1fd.zip
cpython-8b03f943c37e07fb2394acdcfacd066647f9b1fd.tar.gz
cpython-8b03f943c37e07fb2394acdcfacd066647f9b1fd.tar.bz2
bpo-38093: Correctly returns AsyncMock for async subclasses. (GH-15947)
Diffstat (limited to 'Lib/unittest/mock.py')
-rw-r--r--Lib/unittest/mock.py18
1 files changed, 12 insertions, 6 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 4cf8e60..0a16e26 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -983,9 +983,13 @@ class NonCallableMock(Base):
_type = type(self)
if issubclass(_type, MagicMock) and _new_name in _async_method_magics:
klass = AsyncMock
- if issubclass(_type, AsyncMockMixin):
+ elif _new_name in _sync_async_magics:
+ # Special case these ones b/c users will assume they are async,
+ # but they are actually sync (ie. __aiter__)
klass = MagicMock
- if not issubclass(_type, CallableMixin):
+ elif issubclass(_type, AsyncMockMixin):
+ klass = AsyncMock
+ elif not issubclass(_type, CallableMixin):
if issubclass(_type, NonCallableMagicMock):
klass = MagicMock
elif issubclass(_type, NonCallableMock) :
@@ -1881,7 +1885,7 @@ _non_defaults = {
'__reduce__', '__reduce_ex__', '__getinitargs__', '__getnewargs__',
'__getstate__', '__setstate__', '__getformat__', '__setformat__',
'__repr__', '__dir__', '__subclasses__', '__format__',
- '__getnewargs_ex__', '__aenter__', '__aexit__', '__anext__', '__aiter__',
+ '__getnewargs_ex__',
}
@@ -1900,10 +1904,12 @@ _magics = {
# Magic methods used for async `with` statements
_async_method_magics = {"__aenter__", "__aexit__", "__anext__"}
-# `__aiter__` is a plain function but used with async calls
-_async_magics = _async_method_magics | {"__aiter__"}
+# Magic methods that are only used with async calls but are synchronous functions themselves
+_sync_async_magics = {"__aiter__"}
+_async_magics = _async_method_magics | _sync_async_magics
-_all_magics = _magics | _non_defaults
+_all_sync_magics = _magics | _non_defaults
+_all_magics = _all_sync_magics | _async_magics
_unsupported_magics = {
'__getattr__', '__setattr__',