File tree Expand file tree Collapse file tree 3 files changed +20
-3
lines changed Expand file tree Collapse file tree 3 files changed +20
-3
lines changed Original file line number Diff line number Diff line change 46
46
_safe_super = super
47
47
48
48
def _is_async_obj (obj ):
49
- if getattr (obj , '__code__' , None ):
50
- return asyncio .iscoroutinefunction (obj ) or inspect .isawaitable (obj )
51
- else :
49
+ if _is_instance_mock (obj ) and not isinstance (obj , AsyncMock ):
52
50
return False
51
+ return asyncio .iscoroutinefunction (obj ) or inspect .isawaitable (obj )
53
52
54
53
55
54
def _is_async_func (func ):
Original file line number Diff line number Diff line change @@ -18,6 +18,10 @@ async def async_method(self):
18
18
def normal_method (self ):
19
19
pass
20
20
21
+ class AwaitableClass :
22
+ def __await__ (self ):
23
+ yield
24
+
21
25
async def async_func ():
22
26
pass
23
27
@@ -160,6 +164,10 @@ def test_create_autospec_instance(self):
160
164
with self .assertRaises (RuntimeError ):
161
165
create_autospec (async_func , instance = True )
162
166
167
+ def test_create_autospec_awaitable_class (self ):
168
+ awaitable_mock = create_autospec (spec = AwaitableClass ())
169
+ self .assertIsInstance (create_autospec (awaitable_mock ), AsyncMock )
170
+
163
171
def test_create_autospec (self ):
164
172
spec = create_autospec (async_func_args )
165
173
awaitable = spec (1 , 2 , c = 3 )
@@ -321,6 +329,13 @@ def test_is_child_AsyncMock(self):
321
329
self .assertIsInstance (mock .normal_method , MagicMock )
322
330
self .assertIsInstance (mock , MagicMock )
323
331
332
+ def test_magicmock_lambda_spec (self ):
333
+ mock_obj = MagicMock ()
334
+ mock_obj .mock_func = MagicMock (spec = lambda x : x )
335
+
336
+ with patch .object (mock_obj , "mock_func" ) as cm :
337
+ self .assertIsInstance (cm , MagicMock )
338
+
324
339
325
340
class AsyncArguments (unittest .TestCase ):
326
341
def test_add_return_value (self ):
Original file line number Diff line number Diff line change
1
+ Remove `__code__ ` check in AsyncMock that incorrectly
2
+ evaluated function specs as async objects but failed to evaluate classes
3
+ with `__await__ ` but no `__code__ ` attribute defined as async objects.
You can’t perform that action at this time.
0 commit comments