def create_autospec(spec, spec_set=False, instance=False, _parent=None,<EOL>_name=None, **kwargs):
if _is_list(spec):<EOL><INDENT>spec = type(spec)<EOL><DEDENT>is_type = isinstance(spec, ClassTypes)<EOL>_kwargs = {'<STR_LIT>': spec}<EOL>if spec_set:<EOL><INDENT>_kwargs = {'<STR_LIT>': spec}<EOL><DEDENT>elif spec is None:<EOL><INDENT>_kwargs = {}<EOL><DEDENT>_kwargs.update(kwargs)<EOL>Klass = MagicMock<EOL>if type(spec) in DescriptorTypes:<EOL><INDENT>_kwargs = {}<EOL><DEDENT>elif not _callable(spec):<EOL><INDENT>Klass = NonCallableMagicMock<EOL><DEDENT>elif is_type and instance and not _instance_callable(spec):<EOL><INDENT>Klass = NonCallableMagicMock<EOL><DEDENT>_new_name = _name<EOL>if _parent is None:<EOL><INDENT>_new_name = '<STR_LIT>'<EOL><DEDENT>mock = Klass(parent=_parent, _new_parent=_parent, _new_name=_new_name,<EOL>name=_name, **_kwargs)<EOL>if isinstance(spec, FunctionTypes):<EOL><INDENT>mock = _set_signature(mock, spec)<EOL><DEDENT>else:<EOL><INDENT>_check_signature(spec, mock, is_type, instance)<EOL><DEDENT>if _parent is not None and not instance:<EOL><INDENT>_parent._mock_children[_name] = mock<EOL><DEDENT>if is_type and not instance and '<STR_LIT>' not in kwargs:<EOL><INDENT>mock.return_value = create_autospec(spec, spec_set, instance=True,<EOL>_name='<STR_LIT>', _parent=mock)<EOL><DEDENT>for entry in dir(spec):<EOL><INDENT>if _is_magic(entry):<EOL><INDENT>continue<EOL><DEDENT>if isinstance(spec, FunctionTypes) and entry in FunctionAttributes:<EOL><INDENT>continue<EOL><DEDENT>try:<EOL><INDENT>original = getattr(spec, entry)<EOL><DEDENT>except AttributeError:<EOL><INDENT>continue<EOL><DEDENT>kwargs = {'<STR_LIT>': original}<EOL>if spec_set:<EOL><INDENT>kwargs = {'<STR_LIT>': original}<EOL><DEDENT>if not isinstance(original, FunctionTypes):<EOL><INDENT>new = _SpecState(original, spec_set, mock, entry, instance)<EOL>mock._mock_children[entry] = new<EOL><DEDENT>else:<EOL><INDENT>parent = mock<EOL>if isinstance(spec, FunctionTypes):<EOL><INDENT>parent = mock.mock<EOL><DEDENT>new = MagicMock(parent=parent, name=entry, _new_name=entry,<EOL>_new_parent=parent, **kwargs)<EOL>mock._mock_children[entry] = new<EOL>skipfirst = _must_skip(spec, entry, is_type)<EOL>_check_signature(original, new, skipfirst=skipfirst)<EOL><DEDENT>if isinstance(new, FunctionTypes):<EOL><INDENT>setattr(mock, entry, new)<EOL><DEDENT><DEDENT>return mock<EOL>
Create a mock object using another object as a spec. Attributes on the
    mock will use the corresponding attribute on the `spec` object as their
    spec.

    Functions or methods being mocked will have their arguments checked
    to check that they are called with the correct signature.

    If `spec_set` is True then attempting to set attributes that don't exist
    on the spec object will raise an `AttributeError`.

    If a class is used as a spec then the return value of the mock (the
    instance of the class) will have the same spec. You can use a class as the
    spec for an instance object by passing `instance=True`. The returned mock
    will only be callable if instances of the mock are callable.

    `create_autospec` also takes arbitrary keyword arguments that are passed to
    the constructor of the created mock.