mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-38093: Correctly returns AsyncMock for async subclasses. (GH-15947)
This commit is contained in:
parent
2702638eab
commit
8b03f943c3
5 changed files with 178 additions and 67 deletions
|
@ -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__',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue