Lines Matching full:spec
478 self, spec=None, wraps=None, name=None, spec_set=None,
492 spec = spec_set
495 self._mock_add_spec(spec, spec_set)
513 spec, wraps, name, spec_set, parent,
531 def mock_add_spec(self, spec, spec_set=False):
532 """Add a spec to a mock. `spec` can either be an object or a
533 list of strings. Only attributes on the `spec` can be fetched as
536 If `spec_set` is True then only attributes on the spec can be set."""
537 self._mock_add_spec(spec, spec_set)
540 def _mock_add_spec(self, spec, spec_set):
543 if spec is not None and not _is_list(spec):
544 if isinstance(spec, ClassTypes):
545 _spec_class = spec
547 _spec_class = _get_class(spec)
549 spec = dir(spec)
554 __dict__['_mock_methods'] = spec
680 result.spec, result.spec_set, result.instance,
726 spec_string = ' spec=%r'
933 def __init__(self, spec=None, side_effect=None, return_value=DEFAULT,
939 spec, wraps, name, spec_set, parent,
1036 * `spec`: This can be either a list of strings or an existing object (a
1042 If `spec` is an object (rather than a list of strings) then
1043 `mock.__class__` returns the class of the spec object. This allows mocks
1046 * `spec_set`: A stricter variant of `spec`. If used, attempting to *set*
1117 self, getter, attribute, new, spec, create,
1134 self.spec = spec
1145 self.getter, self.attribute, self.new, self.spec,
1249 new, spec, spec_set = self.new, self.spec, self.spec_set
1255 if spec is False:
1256 spec = None
1262 if spec is not None and autospec is not None:
1263 raise TypeError("Can't specify spec and autospec")
1264 if ((spec is not None or autospec is not None) and
1266 raise TypeError("Can't provide explicit spec_set *and* spec or autospec")
1272 if spec is True:
1273 # set spec to the object we are replacing
1274 spec = original
1277 spec = None
1278 elif spec is not None:
1280 spec_set = spec
1281 spec = None
1285 if spec is not None or spec_set is not None:
1287 raise TypeError("Can't use 'spec' with create=True")
1289 # If we're patching out a class and there is a spec
1296 elif spec is not None or spec_set is not None:
1297 this_spec = spec
1307 if spec is not None:
1308 _kwargs['spec'] = spec
1322 # spec is not a list
1323 this_spec = spec
1334 # spec is ignored, new *must* be default, spec_set is treated
1335 # as a boolean. Should we check spec is not None and that spec_set
1419 target, attribute, new=DEFAULT, spec=None,
1424 patch.object(target, attribute, new=DEFAULT, spec=None, create=False,
1431 manager. Arguments `new`, `spec`, `create`, `spec_set`,
1441 getter, attribute, new, spec, create,
1446 def _patch_multiple(target, spec=None, create=False, spec_set=None,
1461 manager. The arguments `spec`, `spec_set`, `create`,
1481 getter, attribute, new, spec, create, spec_set,
1487 getter, attribute, new, spec, create, spec_set,
1496 target, new=DEFAULT, spec=None, create=False,
1517 The `spec` and `spec_set` keyword arguments are passed to the `MagicMock`
1520 In addition you can pass `spec=True` or `spec_set=True`, which causes
1521 patch to pass in the object being mocked as the spec
1527 A more powerful form of `spec` is `autospec`. If you set `autospec=True`
1528 then the mock with be created with a spec from the object being replaced.
1529 All attributes of the mock will also have the spec of the corresponding
1533 their return value (the 'instance') will have the same spec as the class.
1536 arbitrary object as the spec instead of the one being replaced.
1566 getter, attribute, new, spec, create,
1879 def mock_add_spec(self, spec, spec_set=False):
1880 """Add a spec to a mock. `spec` can either be an object or a
1881 list of strings. Only attributes on the `spec` can be fetched as
1884 If `spec_set` is True then only attributes on the spec can be set."""
1885 self._mock_add_spec(spec, spec_set)
1896 If you use the `spec` or `spec_set` arguments then *only* magic
1897 methods that exist in the spec will be created.
1901 def mock_add_spec(self, spec, spec_set=False):
1902 """Add a spec to a mock. `spec` can either be an object or a
1903 list of strings. Only attributes on the `spec` can be fetched as
1906 If `spec_set` is True then only attributes on the spec can be set."""
1907 self._mock_add_spec(spec, spec_set)
2135 def create_autospec(spec, spec_set=False, instance=False, _parent=None,
2137 """Create a mock object using another object as a spec. Attributes on the
2138 mock will use the corresponding attribute on the `spec` object as their
2139 spec.
2145 on the spec object will raise an `AttributeError`.
2147 If a class is used as a spec then the return value of the mock (the
2148 instance of the class) will have the same spec. You can use a class as the
2149 spec for an instance object by passing `instance=True`. The returned mock
2154 if _is_list(spec):
2157 spec = type(spec)
2159 is_type = isinstance(spec, ClassTypes)
2161 _kwargs = {'spec': spec}
2163 _kwargs = {'spec_set': spec}
2164 elif spec is None:
2165 # None we mock with a normal mock without a spec
2171 if type(spec) in DescriptorTypes:
2172 # descriptors don't have a spec
2175 elif not _callable(spec):
2177 elif is_type and instance and not _instance_callable(spec):
2188 if isinstance(spec, FunctionTypes):
2191 mock = _set_signature(mock, spec)
2193 _check_signature(spec, mock, is_type, instance)
2199 mock.return_value = create_autospec(spec, spec_set, instance=True,
2202 for entry in dir(spec):
2207 if isinstance(spec, FunctionTypes) and entry in FunctionAttributes:
2221 original = getattr(spec, entry)
2225 kwargs = {'spec': original}
2234 if isinstance(spec, FunctionTypes):
2240 skipfirst = _must_skip(spec, entry, is_type)
2253 def _must_skip(spec, entry, is_type):
2254 if not isinstance(spec, ClassTypes):
2255 if entry in getattr(spec, '__dict__', {}):
2258 spec = spec.__class__
2259 if not hasattr(spec, '__mro__'):
2263 for klass in spec.__mro__:
2286 def __init__(self, spec, spec_set=False, parent=None,
2288 self.spec = spec
2341 mock = MagicMock(name='open', spec=open)
2343 handle = MagicMock(spec=file_spec)