@@ -414,15 +414,18 @@ class NonCallableMock(Base):
414
414
# necessary.
415
415
_lock = RLock ()
416
416
417
- def __new__ (cls , * args , ** kw ):
417
+ def __new__ (
418
+ cls , spec = None , wraps = None , name = None , spec_set = None ,
419
+ parent = None , _spec_state = None , _new_name = '' , _new_parent = None ,
420
+ _spec_as_instance = False , _eat_self = None , unsafe = False , ** kwargs
421
+ ):
418
422
# every instance has its own class
419
423
# so we can create magic methods on the
420
424
# class without stomping on other mocks
421
425
bases = (cls ,)
422
426
if not issubclass (cls , AsyncMockMixin ):
423
427
# Check if spec is an async object or function
424
- bound_args = _MOCK_SIG .bind_partial (cls , * args , ** kw ).arguments
425
- spec_arg = bound_args .get ('spec_set' , bound_args .get ('spec' ))
428
+ spec_arg = spec_set or spec
426
429
if spec_arg is not None and _is_async_obj (spec_arg ):
427
430
bases = (AsyncMockMixin , cls )
428
431
new = type (cls .__name__ , bases , {'__doc__' : cls .__doc__ })
@@ -508,10 +511,6 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
508
511
_spec_signature = None
509
512
_spec_asyncs = []
510
513
511
- for attr in dir (spec ):
512
- if iscoroutinefunction (getattr (spec , attr , None )):
513
- _spec_asyncs .append (attr )
514
-
515
514
if spec is not None and not _is_list (spec ):
516
515
if isinstance (spec , type ):
517
516
_spec_class = spec
@@ -521,7 +520,13 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
521
520
_spec_as_instance , _eat_self )
522
521
_spec_signature = res and res [1 ]
523
522
524
- spec = dir (spec )
523
+ spec_list = dir (spec )
524
+
525
+ for attr in spec_list :
526
+ if iscoroutinefunction (getattr (spec , attr , None )):
527
+ _spec_asyncs .append (attr )
528
+
529
+ spec = spec_list
525
530
526
531
__dict__ = self .__dict__
527
532
__dict__ ['_spec_class' ] = _spec_class
@@ -1065,9 +1070,6 @@ def _calls_repr(self, prefix="Calls"):
1065
1070
return f"\n { prefix } : { safe_repr (self .mock_calls )} ."
1066
1071
1067
1072
1068
- _MOCK_SIG = inspect .signature (NonCallableMock .__init__ )
1069
-
1070
-
1071
1073
class _AnyComparer (list ):
1072
1074
"""A list which checks if it contains a call which may have an
1073
1075
argument of ANY, flipping the components of item and self from
@@ -2172,10 +2174,7 @@ def mock_add_spec(self, spec, spec_set=False):
2172
2174
2173
2175
2174
2176
class AsyncMagicMixin (MagicMixin ):
2175
- def __init__ (self , * args , ** kw ):
2176
- self ._mock_set_magics () # make magic work for kwargs in init
2177
- _safe_super (AsyncMagicMixin , self ).__init__ (* args , ** kw )
2178
- self ._mock_set_magics () # fix magic broken by upper level init
2177
+ pass
2179
2178
2180
2179
2181
2180
class MagicMock (MagicMixin , Mock ):
@@ -2218,6 +2217,10 @@ def __get__(self, obj, _type=None):
2218
2217
return self .create_mock ()
2219
2218
2220
2219
2220
+ _CODE_ATTRS = dir (CodeType )
2221
+ _CODE_SIG = inspect .signature (partial (CodeType .__init__ , None ))
2222
+
2223
+
2221
2224
class AsyncMockMixin (Base ):
2222
2225
await_count = _delegating_property ('await_count' )
2223
2226
await_args = _delegating_property ('await_args' )
@@ -2235,7 +2238,9 @@ def __init__(self, *args, **kwargs):
2235
2238
self .__dict__ ['_mock_await_count' ] = 0
2236
2239
self .__dict__ ['_mock_await_args' ] = None
2237
2240
self .__dict__ ['_mock_await_args_list' ] = _CallList ()
2238
- code_mock = NonCallableMock (spec_set = CodeType )
2241
+ code_mock = NonCallableMock (spec_set = _CODE_ATTRS )
2242
+ code_mock .__dict__ ["_spec_class" ] = CodeType
2243
+ code_mock .__dict__ ["_spec_signature" ] = _CODE_SIG
2239
2244
code_mock .co_flags = inspect .CO_COROUTINE
2240
2245
self .__dict__ ['__code__' ] = code_mock
2241
2246
self .__dict__ ['__name__' ] = 'AsyncMock'
0 commit comments