Lines Matching refs:spec

42     """Indicates that an invalid value was used as a mock spec."""
426 # Check if spec is an async object or function
428 spec_arg = bound_args.get('spec_set', bound_args.get('spec'))
437 self, spec=None, wraps=None, name=None, spec_set=None,
452 spec = spec_set
457 self._mock_add_spec(spec, spec_set, _spec_as_instance, _eat_self)
476 spec, wraps, name, spec_set, parent,
496 def mock_add_spec(self, spec, spec_set=False):
497 """Add a spec to a mock. `spec` can either be an object or a
498 list of strings. Only attributes on the `spec` can be fetched as
501 If `spec_set` is True then only attributes on the spec can be set."""
502 self._mock_add_spec(spec, spec_set)
505 def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
507 if _is_instance_mock(spec):
508 raise InvalidSpecError(f'Cannot spec a Mock object. [object={spec!r}]')
514 for attr in dir(spec):
515 if iscoroutinefunction(getattr(spec, attr, None)):
518 if spec is not None and not _is_list(spec):
519 if isinstance(spec, type):
520 _spec_class = spec
522 _spec_class = type(spec)
523 res = _get_signature_object(spec,
527 spec = dir(spec)
533 __dict__['_mock_methods'] = spec
659 f"{name!r} is not a valid assertion. Use a spec "
682 result.spec, result.spec_set, result.instance,
690 f'[target={self!r}, attr={result.spec!r}]')
732 spec_string = ' spec=%r'
840 so if we get a _SpecState then no attributes of the spec were accessed
869 This is a best effort method which relies on the spec's signature,
1102 def __init__(self, spec=None, side_effect=None, return_value=DEFAULT,
1107 spec, wraps, name, spec_set, parent,
1209 * `spec`: This can be either a list of strings or an existing object (a
1215 If `spec` is an object (rather than a list of strings) then
1216 `mock.__class__` returns the class of the spec object. This allows mocks
1219 * `spec_set`: A stricter variant of `spec`. If used, attempting to *set*
1278 self, getter, attribute, new, spec, create,
1292 if _is_instance_mock(spec):
1294 f'Cannot spec attr {attribute!r} as the spec '
1295 f'has already been mocked out. [spec={spec!r}]')
1298 f'Cannot spec attr {attribute!r} as the spec_set '
1305 self.spec = spec
1316 self.getter, self.attribute, self.new, self.spec,
1424 new, spec, spec_set = self.new, self.spec, self.spec_set
1430 if spec is False:
1431 spec = None
1437 if spec is not None and autospec is not None:
1438 raise TypeError("Can't specify spec and autospec")
1439 if ((spec is not None or autospec is not None) and
1441 raise TypeError("Can't provide explicit spec_set *and* spec or autospec")
1447 if spec is True:
1448 # set spec to the object we are replacing
1449 spec = original
1452 spec = None
1453 elif spec is not None:
1455 spec_set = spec
1456 spec = None
1460 if spec is not None or spec_set is not None:
1462 raise TypeError("Can't use 'spec' with create=True")
1464 # If we're patching out a class and there is a spec
1466 if spec is None and _is_async_obj(original):
1473 elif spec is not None or spec_set is not None:
1474 this_spec = spec
1486 if spec is not None:
1487 _kwargs['spec'] = spec
1501 # spec is not a list
1502 this_spec = spec
1513 # spec is ignored, new *must* be default, spec_set is treated
1514 # as a boolean. Should we check spec is not None and that spec_set
1618 target, attribute, new=DEFAULT, spec=None,
1627 manager. Arguments `new`, `spec`, `create`, `spec_set`,
1641 getter, attribute, new, spec, create,
1646 def _patch_multiple(target, spec=None, create=False, spec_set=None,
1661 manager. The arguments `spec`, `spec_set`, `create`,
1681 getter, attribute, new, spec, create, spec_set,
1687 getter, attribute, new, spec, create, spec_set,
1696 target, new=DEFAULT, spec=None, create=False,
1718 The `spec` and `spec_set` keyword arguments are passed to the `MagicMock`
1721 In addition you can pass `spec=True` or `spec_set=True`, which causes
1722 patch to pass in the object being mocked as the spec/spec_set object.
1728 A more powerful form of `spec` is `autospec`. If you set `autospec=True`
1729 then the mock will be created with a spec from the object being replaced.
1730 All attributes of the mock will also have the spec of the corresponding
1734 their return value (the 'instance') will have the same spec as the class.
1737 arbitrary object as the spec instead of the one being replaced.
1772 getter, attribute, new, spec, create,
2136 def mock_add_spec(self, spec, spec_set=False):
2137 """Add a spec to a mock. `spec` can either be an object or a
2138 list of strings. Only attributes on the `spec` can be fetched as
2141 If `spec_set` is True then only attributes on the spec can be set."""
2142 self._mock_add_spec(spec, spec_set)
2158 If you use the `spec` or `spec_set` arguments then *only* magic
2159 methods that exist in the spec will be created.
2163 def mock_add_spec(self, spec, spec_set=False):
2164 """Add a spec to a mock. `spec` can either be an object or a
2165 list of strings. Only attributes on the `spec` can be fetched as
2168 If `spec_set` is True then only attributes on the spec can be set."""
2169 self._mock_add_spec(spec, spec_set)
2652 def create_autospec(spec, spec_set=False, instance=False, _parent=None,
2654 """Create a mock object using another object as a spec. Attributes on the
2655 mock will use the corresponding attribute on the `spec` object as their
2656 spec.
2662 on the spec object will raise an `AttributeError`.
2664 If a class is used as a spec then the return value of the mock (the
2665 instance of the class) will have the same spec. You can use a class as the
2666 spec for an instance object by passing `instance=True`. The returned mock
2675 if _is_list(spec):
2678 spec = type(spec)
2680 is_type = isinstance(spec, type)
2681 if _is_instance_mock(spec):
2683 f'[object={spec!r}]')
2684 is_async_func = _is_async_func(spec)
2685 _kwargs = {'spec': spec}
2687 _kwargs = {'spec_set': spec}
2688 elif spec is None:
2689 # None we mock with a normal mock without a spec
2699 if inspect.isdatadescriptor(spec):
2700 # descriptors don't have a spec
2708 elif not _callable(spec):
2710 elif is_type and instance and not _instance_callable(spec):
2723 if isinstance(spec, FunctionTypes):
2726 mock = _set_signature(mock, spec)
2730 _check_signature(spec, mock, is_type, instance)
2736 mock.return_value = create_autospec(spec, spec_set, instance=True,
2739 for entry in dir(spec):
2754 original = getattr(spec, entry)
2758 kwargs = {'spec': original}
2767 if isinstance(spec, FunctionTypes):
2770 skipfirst = _must_skip(spec, entry, is_type)
2792 def _must_skip(spec, entry, is_type):
2794 Return whether we should skip the first argument on spec's `entry`
2797 if not isinstance(spec, type):
2798 if entry in getattr(spec, '__dict__', {}):
2801 spec = spec.__class__
2803 for klass in spec.__mro__:
2822 def __init__(self, spec, spec_set=False, parent=None,
2824 self.spec = spec
2903 mock = MagicMock(name='open', spec=open_spec)
2905 handle = MagicMock(spec=file_spec)
2959 and all assigned mocks without a name or spec will be sealed.