bpo-38093: Correctly returns AsyncMock for async subclasses. (GH-15947)

This commit is contained in:
Lisa Roach 2019-09-19 21:04:18 -07:00 committed by GitHub
parent 2702638eab
commit 8b03f943c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 178 additions and 67 deletions

View file

@ -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__',