@@ -401,15 +401,18 @@ def __init__(self, /, *args, **kwargs):
401
401
class NonCallableMock (Base ):
402
402
"""A non-callable version of `Mock`"""
403
403
404
- def __new__ (cls , / , * args , ** kw ):
404
+ def __new__ (
405
+ cls , spec = None , wraps = None , name = None , spec_set = None ,
406
+ parent = None , _spec_state = None , _new_name = '' , _new_parent = None ,
407
+ _spec_as_instance = False , _eat_self = None , unsafe = False , ** kwargs
408
+ ):
405
409
# every instance has its own class
406
410
# so we can create magic methods on the
407
411
# class without stomping on other mocks
408
412
bases = (cls ,)
409
413
if not issubclass (cls , AsyncMockMixin ):
410
414
# Check if spec is an async object or function
411
- bound_args = _MOCK_SIG .bind_partial (cls , * args , ** kw ).arguments
412
- spec_arg = bound_args .get ('spec_set' , bound_args .get ('spec' ))
415
+ spec_arg = spec_set or spec
413
416
if spec_arg is not None and _is_async_obj (spec_arg ):
414
417
bases = (AsyncMockMixin , cls )
415
418
new = type (cls .__name__ , bases , {'__doc__' : cls .__doc__ })
@@ -490,11 +493,6 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
490
493
_eat_self = False ):
491
494
_spec_class = None
492
495
_spec_signature = None
493
- _spec_asyncs = []
494
-
495
- for attr in dir (spec ):
496
- if iscoroutinefunction (getattr (spec , attr , None )):
497
- _spec_asyncs .append (attr )
498
496
499
497
if spec is not None and not _is_list (spec ):
500
498
if isinstance (spec , type ):
@@ -512,7 +510,6 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
512
510
__dict__ ['_spec_set' ] = spec_set
513
511
__dict__ ['_spec_signature' ] = _spec_signature
514
512
__dict__ ['_mock_methods' ] = spec
515
- __dict__ ['_spec_asyncs' ] = _spec_asyncs
516
513
517
514
def __get_return_value (self ):
518
515
ret = self ._mock_return_value
@@ -1001,7 +998,8 @@ def _get_child_mock(self, /, **kw):
1001
998
For non-callable mocks the callable variant will be used (rather than
1002
999
any custom subclass)."""
1003
1000
_new_name = kw .get ("_new_name" )
1004
- if _new_name in self .__dict__ ['_spec_asyncs' ]:
1001
+ _spec_val = getattr (self .__dict__ ["_spec_class" ], _new_name , None )
1002
+ if _spec_val is not None and asyncio .iscoroutinefunction (_spec_val ):
1005
1003
return AsyncMock (** kw )
1006
1004
1007
1005
if self ._mock_sealed :
@@ -1043,9 +1041,6 @@ def _calls_repr(self, prefix="Calls"):
1043
1041
return f"\n { prefix } : { safe_repr (self .mock_calls )} ."
1044
1042
1045
1043
1046
- _MOCK_SIG = inspect .signature (NonCallableMock .__init__ )
1047
-
1048
-
1049
1044
class _AnyComparer (list ):
1050
1045
"""A list which checks if it contains a call which may have an
1051
1046
argument of ANY, flipping the components of item and self from
@@ -2121,10 +2116,8 @@ def mock_add_spec(self, spec, spec_set=False):
2121
2116
2122
2117
2123
2118
class AsyncMagicMixin (MagicMixin ):
2124
- def __init__ (self , / , * args , ** kw ):
2125
- self ._mock_set_magics () # make magic work for kwargs in init
2126
- _safe_super (AsyncMagicMixin , self ).__init__ (* args , ** kw )
2127
- self ._mock_set_magics () # fix magic broken by upper level init
2119
+ pass
2120
+
2128
2121
2129
2122
class MagicMock (MagicMixin , Mock ):
2130
2123
"""
@@ -2166,6 +2159,10 @@ def __get__(self, obj, _type=None):
2166
2159
return self .create_mock ()
2167
2160
2168
2161
2162
+ _CODE_ATTRS = dir (CodeType )
2163
+ _CODE_SIG = inspect .signature (partial (CodeType .__init__ , None ))
2164
+
2165
+
2169
2166
class AsyncMockMixin (Base ):
2170
2167
await_count = _delegating_property ('await_count' )
2171
2168
await_args = _delegating_property ('await_args' )
@@ -2183,7 +2180,9 @@ def __init__(self, /, *args, **kwargs):
2183
2180
self .__dict__ ['_mock_await_count' ] = 0
2184
2181
self .__dict__ ['_mock_await_args' ] = None
2185
2182
self .__dict__ ['_mock_await_args_list' ] = _CallList ()
2186
- code_mock = NonCallableMock (spec_set = CodeType )
2183
+ code_mock = NonCallableMock (spec_set = _CODE_ATTRS )
2184
+ code_mock .__dict__ ["_spec_class" ] = CodeType
2185
+ code_mock .__dict__ ["_spec_signature" ] = _CODE_SIG
2187
2186
code_mock .co_flags = inspect .CO_COROUTINE
2188
2187
self .__dict__ ['__code__' ] = code_mock
2189
2188
0 commit comments