@@ -411,15 +411,18 @@ class NonCallableMock(Base):
411
411
# necessary.
412
412
_lock = RLock ()
413
413
414
- def __new__ (cls , / , * args , ** kw ):
414
+ def __new__ (
415
+ cls , spec = None , wraps = None , name = None , spec_set = None ,
416
+ parent = None , _spec_state = None , _new_name = '' , _new_parent = None ,
417
+ _spec_as_instance = False , _eat_self = None , unsafe = False , ** kwargs
418
+ ):
415
419
# every instance has its own class
416
420
# so we can create magic methods on the
417
421
# class without stomping on other mocks
418
422
bases = (cls ,)
419
423
if not issubclass (cls , AsyncMockMixin ):
420
424
# Check if spec is an async object or function
421
- bound_args = _MOCK_SIG .bind_partial (cls , * args , ** kw ).arguments
422
- spec_arg = bound_args .get ('spec_set' , bound_args .get ('spec' ))
425
+ spec_arg = spec_set or spec
423
426
if spec_arg is not None and _is_async_obj (spec_arg ):
424
427
bases = (AsyncMockMixin , cls )
425
428
new = type (cls .__name__ , bases , {'__doc__' : cls .__doc__ })
@@ -505,10 +508,6 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
505
508
_spec_signature = None
506
509
_spec_asyncs = []
507
510
508
- for attr in dir (spec ):
509
- if iscoroutinefunction (getattr (spec , attr , None )):
510
- _spec_asyncs .append (attr )
511
-
512
511
if spec is not None and not _is_list (spec ):
513
512
if isinstance (spec , type ):
514
513
_spec_class = spec
@@ -518,7 +517,13 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
518
517
_spec_as_instance , _eat_self )
519
518
_spec_signature = res and res [1 ]
520
519
521
- spec = dir (spec )
520
+ spec_list = dir (spec )
521
+
522
+ for attr in spec_list :
523
+ if iscoroutinefunction (getattr (spec , attr , None )):
524
+ _spec_asyncs .append (attr )
525
+
526
+ spec = spec_list
522
527
523
528
__dict__ = self .__dict__
524
529
__dict__ ['_spec_class' ] = _spec_class
@@ -1057,9 +1062,6 @@ def _calls_repr(self, prefix="Calls"):
1057
1062
return f"\n { prefix } : { safe_repr (self .mock_calls )} ."
1058
1063
1059
1064
1060
- _MOCK_SIG = inspect .signature (NonCallableMock .__init__ )
1061
-
1062
-
1063
1065
class _AnyComparer (list ):
1064
1066
"""A list which checks if it contains a call which may have an
1065
1067
argument of ANY, flipping the components of item and self from
@@ -2138,10 +2140,8 @@ def mock_add_spec(self, spec, spec_set=False):
2138
2140
2139
2141
2140
2142
class AsyncMagicMixin (MagicMixin ):
2141
- def __init__ (self , / , * args , ** kw ):
2142
- self ._mock_set_magics () # make magic work for kwargs in init
2143
- _safe_super (AsyncMagicMixin , self ).__init__ (* args , ** kw )
2144
- self ._mock_set_magics () # fix magic broken by upper level init
2143
+ pass
2144
+
2145
2145
2146
2146
class MagicMock (MagicMixin , Mock ):
2147
2147
"""
@@ -2183,6 +2183,10 @@ def __get__(self, obj, _type=None):
2183
2183
return self .create_mock ()
2184
2184
2185
2185
2186
+ _CODE_ATTRS = dir (CodeType )
2187
+ _CODE_SIG = inspect .signature (partial (CodeType .__init__ , None ))
2188
+
2189
+
2186
2190
class AsyncMockMixin (Base ):
2187
2191
await_count = _delegating_property ('await_count' )
2188
2192
await_args = _delegating_property ('await_args' )
@@ -2200,7 +2204,9 @@ def __init__(self, /, *args, **kwargs):
2200
2204
self .__dict__ ['_mock_await_count' ] = 0
2201
2205
self .__dict__ ['_mock_await_args' ] = None
2202
2206
self .__dict__ ['_mock_await_args_list' ] = _CallList ()
2203
- code_mock = NonCallableMock (spec_set = CodeType )
2207
+ code_mock = NonCallableMock (spec_set = _CODE_ATTRS )
2208
+ code_mock .__dict__ ["_spec_class" ] = CodeType
2209
+ code_mock .__dict__ ["_spec_signature" ] = _CODE_SIG
2204
2210
code_mock .co_flags = inspect .CO_COROUTINE
2205
2211
self .__dict__ ['__code__' ] = code_mock
2206
2212
self .__dict__ ['__name__' ] = 'AsyncMock'
0 commit comments