Lines Matching refs:self

99         # Skip the `self` argument in __init__
130 def checksig(self, /, *args, **kwargs):
290 def __init__(self, name):
291 self.name = name
293 def __repr__(self):
294 return 'sentinel.%s' % self.name
296 def __reduce__(self):
297 return 'sentinel.%s' % self.name
302 def __init__(self):
303 self._sentinels = {}
305 def __getattr__(self, name):
309 return self._sentinels.setdefault(name, _SentinelObject(name))
311 def __reduce__(self):
332 def _get(self, name=name, _the_name=_the_name):
333 sig = self._mock_delegate
335 return getattr(self, _the_name)
337 def _set(self, value, name=name, _the_name=_the_name):
338 sig = self._mock_delegate
340 self.__dict__[_the_name] = value
350 def __contains__(self, value):
352 return list.__contains__(self, value)
354 len_self = len(self)
359 sub_list = self[i:i+len_value]
364 def __repr__(self):
365 return pprint.pformat(list(self))
396 def __init__(self, obj):
397 self.obj = iter(obj)
398 def __next__(self):
399 return next(self.obj)
404 def __init__(self, /, *args, **kwargs):
437 self, spec=None, wraps=None, name=None, spec_set=None,
444 __dict__ = self.__dict__
457 self._mock_add_spec(spec, spec_set, _spec_as_instance, _eat_self)
473 self.configure_mock(**kwargs)
475 _safe_super(NonCallableMock, self).__init__(
481 def attach_mock(self, mock, attribute):
493 setattr(self, attribute, mock)
496 def mock_add_spec(self, spec, spec_set=False):
502 self._mock_add_spec(spec, spec_set)
505 def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
529 __dict__ = self.__dict__
536 def __get_return_value(self):
537 ret = self._mock_return_value
538 if self._mock_delegate is not None:
539 ret = self._mock_delegate.return_value
542 ret = self._get_child_mock(
543 _new_parent=self, _new_name='()'
545 self.return_value = ret
549 def __set_return_value(self, value):
550 if self._mock_delegate is not None:
551 self._mock_delegate.return_value = value
553 self._mock_return_value = value
554 _check_and_set_parent(self, value, None, '()')
562 def __class__(self):
563 if self._spec_class is None:
564 return type(self)
565 return self._spec_class
574 def __get_side_effect(self):
575 delegated = self._mock_delegate
577 return self._mock_side_effect
585 def __set_side_effect(self, value):
587 delegated = self._mock_delegate
589 self._mock_side_effect = value
596 def reset_mock(self, visited=None,*, return_value=False, side_effect=False):
600 if id(self) in visited:
602 visited.append(id(self))
604 self.called = False
605 self.call_args = None
606 self.call_count = 0
607 self.mock_calls = _CallList()
608 self.call_args_list = _CallList()
609 self.method_calls = _CallList()
612 self._mock_return_value = DEFAULT
614 self._mock_side_effect = None
616 for child in self._mock_children.values():
621 ret = self._mock_return_value
622 if _is_instance_mock(ret) and ret is not self:
626 def configure_mock(self, /, **kwargs):
642 obj = self
648 def __getattr__(self, name):
651 elif self._mock_methods is not None:
652 if name not in self._mock_methods or name in _all_magics:
656 if not self._mock_unsafe and (not self._mock_methods or name not in self._mock_methods):
663 result = self._mock_children.get(name)
668 if self._mock_wraps is not None:
671 wraps = getattr(self._mock_wraps, name)
673 result = self._get_child_mock(
674 parent=self, name=name, wraps=wraps, _new_name=name,
675 _new_parent=self
677 self._mock_children[name] = result
686 target_name = self.__dict__['_mock_name'] or self
690 f'[target={self!r}, attr={result.spec!r}]')
691 self._mock_children[name] = result
696 def _extract_mock_name(self):
697 _name_list = [self._mock_new_name]
698 _parent = self._mock_new_parent
699 last = self
723 def __repr__(self):
724 name = self._extract_mock_name()
731 if self._spec_class is not None:
733 if self._spec_set:
735 spec_string = spec_string % self._spec_class.__name__
737 type(self).__name__,
740 id(self)
744 def __dir__(self):
747 return object.__dir__(self)
749 extras = self._mock_methods or []
750 from_type = dir(type(self))
751 from_dict = list(self.__dict__)
753 m_name for m_name, m_value in self._mock_children.items()
762 def __setattr__(self, name, value):
765 return object.__setattr__(self, name, value)
766 elif (self._spec_set and self._mock_methods is not None and
767 name not in self._mock_methods and
768 name not in self.__dict__):
774 if self._mock_methods is not None and name not in self._mock_methods:
778 setattr(type(self), name, _get_method(name, value))
780 value = lambda *args, **kw: original(self, *args, **kw)
784 _check_and_set_parent(self, value, None, name)
785 setattr(type(self), name, value)
786 self._mock_children[name] = value
788 self._spec_class = value
791 if _check_and_set_parent(self, value, name, name):
792 self._mock_children[name] = value
794 if self._mock_sealed and not hasattr(self, name):
795 mock_name = f'{self._extract_mock_name()}.{name}'
798 return object.__setattr__(self, name, value)
801 def __delattr__(self, name):
802 if name in _all_magics and name in type(self).__dict__:
803 delattr(type(self), name)
804 if name not in self.__dict__:
809 obj = self._mock_children.get(name, _missing)
810 if name in self.__dict__:
811 _safe_super(NonCallableMock, self).__delattr__(name)
815 del self._mock_children[name]
816 self._mock_children[name] = _deleted
819 def _format_mock_call_signature(self, args, kwargs):
820 name = self._mock_name or 'mock'
824 def _format_mock_failure_message(self, args, kwargs, action='call'):
826 expected_string = self._format_mock_call_signature(args, kwargs)
827 call_args = self.call_args
828 actual_string = self._format_mock_call_signature(*call_args)
832 def _get_call_signature_from_name(self, name):
844 return self._spec_signature
848 children = self._mock_children
865 def _call_matcher(self, _call):
874 sig = self._get_call_signature_from_name(_call[0])
876 sig = self._spec_signature
892 def assert_not_called(self):
895 if self.call_count != 0:
897 % (self._mock_name or 'mock',
898 self.call_count,
899 self._calls_repr()))
902 def assert_called(self):
905 if self.call_count == 0:
907 (self._mock_name or 'mock'))
910 def assert_called_once(self):
913 if not self.call_count == 1:
915 % (self._mock_name or 'mock',
916 self.call_count,
917 self._calls_repr()))
920 def assert_called_with(self, /, *args, **kwargs):
925 if self.call_args is None:
926 expected = self._format_mock_call_signature(args, kwargs)
933 msg = self._format_mock_failure_message(args, kwargs)
935 expected = self._call_matcher(_Call((args, kwargs), two=True))
936 actual = self._call_matcher(self.call_args)
942 def assert_called_once_with(self, /, *args, **kwargs):
945 if not self.call_count == 1:
947 % (self._mock_name or 'mock',
948 self.call_count,
949 self._calls_repr()))
951 return self.assert_called_with(*args, **kwargs)
954 def assert_has_calls(self, calls, any_order=False):
964 expected = [self._call_matcher(c) for c in calls]
966 all_calls = _CallList(self._call_matcher(c) for c in self.mock_calls)
979 f'{self._calls_repr(prefix="Actual").rstrip(".")}'
994 'found %r instead' % (self._mock_name or 'mock',
999 def assert_any_call(self, /, *args, **kwargs):
1005 expected = self._call_matcher(_Call((args, kwargs), two=True))
1007 actual = [self._call_matcher(c) for c in self.call_args_list]
1009 expected_string = self._format_mock_call_signature(args, kwargs)
1015 def _get_child_mock(self, /, **kw):
1023 if self._mock_sealed:
1025 mock_name = self._extract_mock_name() + attribute
1029 if _new_name in self.__dict__['_spec_asyncs']:
1032 _type = type(self)
1038 self._mock_methods and _new_name in self._mock_methods):
1053 def _calls_repr(self, prefix="Calls"):
1054 """Renders self.mock_calls as a string.
1058 If self.mock_calls is empty, an empty string is returned. The
1061 if not self.mock_calls:
1063 return f"\n{prefix}: {safe_repr(self.mock_calls)}."
1071 argument of ANY, flipping the components of item and self from
1074 def __contains__(self, item):
1075 for _call in self:
1102 def __init__(self, spec=None, side_effect=None, return_value=DEFAULT,
1105 self.__dict__['_mock_return_value'] = return_value
1106 _safe_super(CallableMixin, self).__init__(
1111 self.side_effect = side_effect
1114 def _mock_check_sig(self, /, *args, **kwargs):
1119 def __call__(self, /, *args, **kwargs):
1120 # can't use self in-case a function / method we are mocking uses self
1122 self._mock_check_sig(*args, **kwargs)
1123 self._increment_mock_call(*args, **kwargs)
1124 return self._mock_call(*args, **kwargs)
1127 def _mock_call(self, /, *args, **kwargs):
1128 return self._execute_mock_call(*args, **kwargs)
1130 def _increment_mock_call(self, /, *args, **kwargs):
1131 self.called = True
1132 self.call_count += 1
1138 self.call_args = _call
1139 self.call_args_list.append(_call)
1142 do_method_calls = self._mock_parent is not None
1143 method_call_name = self._mock_name
1146 mock_call_name = self._mock_new_name
1148 self.mock_calls.append(_Call(('', args, kwargs)))
1151 _new_parent = self._mock_new_parent
1176 def _execute_mock_call(self, /, *args, **kwargs):
1180 effect = self.side_effect
1194 if self._mock_return_value is not DEFAULT:
1195 return self.return_value
1197 if self._mock_wraps is not None:
1198 return self._mock_wraps(*args, **kwargs)
1200 return self.return_value
1278 self, getter, attribute, new, spec, create,
1301 self.getter = getter
1302 self.attribute = attribute
1303 self.new = new
1304 self.new_callable = new_callable
1305 self.spec = spec
1306 self.create = create
1307 self.has_local = False
1308 self.spec_set = spec_set
1309 self.autospec = autospec
1310 self.kwargs = kwargs
1311 self.additional_patchers = []
1314 def copy(self):
1316 self.getter, self.attribute, self.new, self.spec,
1317 self.create, self.spec_set,
1318 self.autospec, self.new_callable, self.kwargs
1320 patcher.attribute_name = self.attribute_name
1322 p.copy() for p in self.additional_patchers
1327 def __call__(self, func):
1329 return self.decorate_class(func)
1331 return self.decorate_async_callable(func)
1332 return self.decorate_callable(func)
1335 def decorate_class(self, klass):
1344 patcher = self.copy()
1350 def decoration_helper(self, patched, args, keywargs):
1364 def decorate_callable(self, func):
1367 func.patchings.append(self)
1372 with self.decoration_helper(patched,
1377 patched.patchings = [self]
1381 def decorate_async_callable(self, func):
1384 func.patchings.append(self)
1389 with self.decoration_helper(patched,
1394 patched.patchings = [self]
1398 def get_original(self):
1399 target = self.getter()
1400 name = self.attribute
1413 self.create = True
1415 if not self.create and original is DEFAULT:
1422 def __enter__(self):
1424 new, spec, spec_set = self.new, self.spec, self.spec_set
1425 autospec, kwargs = self.autospec, self.kwargs
1426 new_callable = self.new_callable
1427 self.target = self.getter()
1443 original, local = self.get_original()
1493 issubclass(Klass, NonCallableMock) and self.attribute):
1494 _kwargs['name'] = self.attribute
1527 if _is_instance_mock(self.target):
1529 f'Cannot autospec attr {self.attribute!r} as the patch '
1531 f'[target={self.target!r}, attr={autospec!r}]')
1533 target_name = getattr(self.target, '__name__', self.target)
1535 f'Cannot autospec attr {self.attribute!r} from target '
1537 f'[target={self.target!r}, attr={autospec!r}]')
1540 _name=self.attribute, **kwargs)
1548 self.temp_original = original
1549 self.is_local = local
1550 self._exit_stack = contextlib.ExitStack()
1552 setattr(self.target, self.attribute, new_attr)
1553 if self.attribute_name is not None:
1555 if self.new is DEFAULT:
1556 extra_args[self.attribute_name] = new
1557 for patching in self.additional_patchers:
1558 arg = self._exit_stack.enter_context(patching)
1565 if not self.__exit__(*sys.exc_info()):
1568 def __exit__(self, *exc_info):
1570 if self.is_local and self.temp_original is not DEFAULT:
1571 setattr(self.target, self.attribute, self.temp_original)
1573 delattr(self.target, self.attribute)
1574 if not self.create and (not hasattr(self.target, self.attribute) or
1575 self.attribute in ('__doc__', '__module__',
1579 setattr(self.target, self.attribute, self.temp_original)
1581 del self.temp_original
1582 del self.is_local
1583 del self.target
1584 exit_stack = self._exit_stack
1585 del self._exit_stack
1589 def start(self):
1591 result = self.__enter__()
1592 self._active_patches.append(self)
1596 def stop(self):
1599 self._active_patches.remove(self)
1604 return self.__exit__(None, None, None)
1806 def __init__(self, in_dict, values=(), clear=False, **kwargs):
1807 self.in_dict = in_dict
1809 self.values = dict(values)
1810 self.values.update(kwargs)
1811 self.clear = clear
1812 self._original = None
1815 def __call__(self, f):
1817 return self.decorate_class(f)
1819 return self.decorate_async_callable(f)
1820 return self.decorate_callable(f)
1823 def decorate_callable(self, f):
1826 self._patch_dict()
1830 self._unpatch_dict()
1835 def decorate_async_callable(self, f):
1838 self._patch_dict()
1842 self._unpatch_dict()
1847 def decorate_class(self, klass):
1852 decorator = _patch_dict(self.in_dict, self.values, self.clear)
1858 def __enter__(self):
1860 self._patch_dict()
1861 return self.in_dict
1864 def _patch_dict(self):
1865 values = self.values
1866 if isinstance(self.in_dict, str):
1867 self.in_dict = pkgutil.resolve_name(self.in_dict)
1868 in_dict = self.in_dict
1869 clear = self.clear
1879 self._original = original
1892 def _unpatch_dict(self):
1893 in_dict = self.in_dict
1894 original = self._original
1905 def __exit__(self, *args):
1907 if self._original is not None:
1908 self._unpatch_dict()
1912 def start(self):
1914 result = self.__enter__()
1915 _patch._active_patches.append(self)
1919 def stop(self):
1922 _patch._active_patches.remove(self)
1927 return self.__exit__(None, None, None)
1988 def method(self, /, *args, **kw):
1989 return func(self, *args, **kw)
2016 '__hash__': lambda self: object.__hash__(self),
2017 '__str__': lambda self: object.__str__(self),
2018 '__sizeof__': lambda self: object.__sizeof__(self),
2019 '__fspath__': lambda self: f"{type(self).__name__}/{self._extract_mock_name()}/{id(self)}",
2039 def _get_eq(self):
2041 ret_val = self.__eq__._mock_return_value
2044 if self is other:
2049 def _get_ne(self):
2051 if self.__ne__._mock_return_value is not DEFAULT:
2053 if self is other:
2058 def _get_iter(self):
2060 ret_val = self.__iter__._mock_return_value
2068 def _get_async_iter(self):
2070 ret_val = self.__aiter__._mock_return_value
2104 def __init__(self, /, *args, **kw):
2105 self._mock_set_magics() # make magic work for kwargs in init
2106 _safe_super(MagicMixin, self).__init__(*args, **kw)
2107 self._mock_set_magics() # fix magic broken by upper level init
2110 def _mock_set_magics(self):
2114 if getattr(self, "_mock_methods", None) is not None:
2115 these_magics = orig_magics.intersection(self._mock_methods)
2121 if entry in type(self).__dict__:
2123 delattr(self, entry)
2126 these_magics = these_magics - set(type(self).__dict__)
2128 _type = type(self)
2130 setattr(_type, entry, MagicProxy(entry, self))
2136 def mock_add_spec(self, spec, spec_set=False):
2142 self._mock_add_spec(spec, spec_set)
2143 self._mock_set_magics()
2147 def __init__(self, /, *args, **kw):
2148 self._mock_set_magics() # make magic work for kwargs in init
2149 _safe_super(AsyncMagicMixin, self).__init__(*args, **kw)
2150 self._mock_set_magics() # fix magic broken by upper level init
2163 def mock_add_spec(self, spec, spec_set=False):
2169 self._mock_add_spec(spec, spec_set)
2170 self._mock_set_magics()
2175 def __init__(self, name, parent):
2176 self.name = name
2177 self.parent = parent
2179 def create_mock(self):
2180 entry = self.name
2181 parent = self.parent
2188 def __get__(self, obj, _type=None):
2189 return self.create_mock()
2197 def __init__(self, /, *args, **kwargs):
2205 self.__dict__['_is_coroutine'] = asyncio.coroutines._is_coroutine
2206 self.__dict__['_mock_await_count'] = 0
2207 self.__dict__['_mock_await_args'] = None
2208 self.__dict__['_mock_await_args_list'] = _CallList()
2219 self.__dict__['__code__'] = code_mock
2220 self.__dict__['__name__'] = 'AsyncMock'
2221 self.__dict__['__defaults__'] = tuple()
2222 self.__dict__['__kwdefaults__'] = {}
2223 self.__dict__['__annotations__'] = None
2225 async def _execute_mock_call(self, /, *args, **kwargs):
2230 self.await_count += 1
2231 self.await_args = _call
2232 self.await_args_list.append(_call)
2234 effect = self.side_effect
2255 if self._mock_return_value is not DEFAULT:
2256 return self.return_value
2258 if self._mock_wraps is not None:
2259 if iscoroutinefunction(self._mock_wraps):
2260 return await self._mock_wraps(*args, **kwargs)
2261 return self._mock_wraps(*args, **kwargs)
2263 return self.return_value
2265 def assert_awaited(self):
2269 if self.await_count == 0:
2270 msg = f"Expected {self._mock_name or 'mock'} to have been awaited."
2273 def assert_awaited_once(self):
2277 if not self.await_count == 1:
2278 msg = (f"Expected {self._mock_name or 'mock'} to have been awaited once."
2279 f" Awaited {self.await_count} times.")
2282 def assert_awaited_with(self, /, *args, **kwargs):
2286 if self.await_args is None:
2287 expected = self._format_mock_call_signature(args, kwargs)
2291 msg = self._format_mock_failure_message(args, kwargs, action='await')
2294 expected = self._call_matcher(_Call((args, kwargs), two=True))
2295 actual = self._call_matcher(self.await_args)
2300 def assert_awaited_once_with(self, /, *args, **kwargs):
2305 if not self.await_count == 1:
2306 msg = (f"Expected {self._mock_name or 'mock'} to have been awaited once."
2307 f" Awaited {self.await_count} times.")
2309 return self.assert_awaited_with(*args, **kwargs)
2311 def assert_any_await(self, /, *args, **kwargs):
2315 expected = self._call_matcher(_Call((args, kwargs), two=True))
2317 actual = [self._call_matcher(c) for c in self.await_args_list]
2319 expected_string = self._format_mock_call_signature(args, kwargs)
2324 def assert_has_awaits(self, calls, any_order=False):
2336 expected = [self._call_matcher(c) for c in calls]
2338 all_awaits = _CallList(self._call_matcher(c) for c in self.await_args_list)
2351 f'Actual: {self.await_args_list}'
2368 def assert_not_awaited(self):
2372 if self.await_count != 0:
2373 msg = (f"Expected {self._mock_name or 'mock'} to not have been awaited."
2374 f" Awaited {self.await_count} times.")
2377 def reset_mock(self, /, *args, **kwargs):
2382 self.await_count = 0
2383 self.await_args = None
2384 self.await_args_list = _CallList()
2433 def __eq__(self, other):
2436 def __ne__(self, other):
2439 def __repr__(self):
2515 def __init__(self, value=(), name=None, parent=None, two=False,
2517 self._mock_name = name
2518 self._mock_parent = parent
2519 self._mock_from_kall = from_kall
2522 def __eq__(self, other):
2529 if len(self) == 2:
2530 self_args, self_kwargs = self
2532 self_name, self_args, self_kwargs = self
2534 if (getattr(self, '_mock_parent', None) and getattr(other, '_mock_parent', None)
2535 and self._mock_parent != other._mock_parent):
2578 def __call__(self, /, *args, **kwargs):
2579 if self._mock_name is None:
2582 name = self._mock_name + '()'
2583 return _Call((self._mock_name, args, kwargs), name=name, parent=self)
2586 def __getattr__(self, attr):
2587 if self._mock_name is None:
2589 name = '%s.%s' % (self._mock_name, attr)
2590 return _Call(name=name, parent=self, from_kall=False)
2593 def __getattribute__(self, attr):
2596 return tuple.__getattribute__(self, attr)
2599 def _get_call_arguments(self):
2600 if len(self) == 2:
2601 args, kwargs = self
2603 name, args, kwargs = self
2608 def args(self):
2609 return self._get_call_arguments()[0]
2612 def kwargs(self):
2613 return self._get_call_arguments()[1]
2615 def __repr__(self):
2616 if not self._mock_from_kall:
2617 name = self._mock_name or 'call'
2622 if len(self) == 2:
2624 args, kwargs = self
2626 name, args, kwargs = self
2636 def call_list(self):
2641 thing = self
2811 # (if looked up on instance, self is already skipped)
2822 def __init__(self, spec, spec_set=False, parent=None,
2824 self.spec = spec
2825 self.ids = ids
2826 self.spec_set = spec_set
2827 self.parent = parent
2828 self.instance = instance
2829 self.name = name
2942 def _get_child_mock(self, /, **kwargs):
2945 def __get__(self, obj, obj_type=None):
2946 return self()
2947 def __set__(self, obj, val):
2948 self(val)
2979 def __init__(self, iterator):
2980 self.iterator = iterator
2983 self.__dict__['__code__'] = code_mock
2985 async def __anext__(self):
2987 return next(self.iterator)