[3.8] bpo-38093: Correctly returns AsyncMock for async subclasses. (GH-15947) (GH-16299)
(cherry picked from commit 8b03f943c37e07fb2394acdcfacd066647f9b1fd)
Co-authored-by: Lisa Roach <lisaroach14@gmail.com>
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 9fd5c3c..0c7545b 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -988,9 +988,13 @@
_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) :
@@ -1867,7 +1871,7 @@
'__reduce__', '__reduce_ex__', '__getinitargs__', '__getnewargs__',
'__getstate__', '__setstate__', '__getformat__', '__setformat__',
'__repr__', '__dir__', '__subclasses__', '__format__',
- '__getnewargs_ex__', '__aenter__', '__aexit__', '__anext__', '__aiter__',
+ '__getnewargs_ex__',
}
@@ -1886,10 +1890,12 @@
# 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__',