MCPcopy Index your code
hub / github.com/python/cpython / _mock_add_spec

Method _mock_add_spec

Lib/unittest/mock.py:534–571  ·  view source on GitHub ↗
(self, spec, spec_set, _spec_as_instance=False,
                       _eat_self=False)

Source from the content-addressed store, hash-verified

532
533
534 def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
535 _eat_self=False):
536 if _is_instance_mock(spec):
537 raise InvalidSpecError(f'Cannot spec a Mock object. [object={spec!r}]')
538
539 _spec_class = None
540 _spec_signature = None
541 _spec_asyncs = []
542
543 if spec is not None and not _is_list(spec):
544 if isinstance(spec, type):
545 _spec_class = spec
546 else:
547 _spec_class = type(spec)
548 res = _get_signature_object(spec,
549 _spec_as_instance, _eat_self)
550 _spec_signature = res and res[1]
551
552 spec_list = dir(spec)
553
554 for attr in spec_list:
555 static_attr = inspect.getattr_static(spec, attr, None)
556 unwrapped_attr = static_attr
557 try:
558 unwrapped_attr = inspect.unwrap(unwrapped_attr)
559 except ValueError:
560 pass
561 if iscoroutinefunction(unwrapped_attr):
562 _spec_asyncs.append(attr)
563
564 spec = spec_list
565
566 __dict__ = self.__dict__
567 __dict__['_spec_class'] = _spec_class
568 __dict__['_spec_set'] = spec_set
569 __dict__['_spec_signature'] = _spec_signature
570 __dict__['_mock_methods'] = spec
571 __dict__['_spec_asyncs'] = _spec_asyncs
572
573 def _mock_extend_spec_methods(self, spec_methods):
574 methods = self.__dict__.get('_mock_methods') or []

Callers 4

__init__Method · 0.95
mock_add_specMethod · 0.95
mock_add_specMethod · 0.80
mock_add_specMethod · 0.80

Calls 7

iscoroutinefunctionFunction · 0.90
_is_instance_mockFunction · 0.85
InvalidSpecErrorClass · 0.85
_is_listFunction · 0.85
_get_signature_objectFunction · 0.85
unwrapMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected