diff -r fe532dccf8f6 Lib/unittest/mock.py --- a/Lib/unittest/mock.py Mon Apr 14 10:30:43 2014 -0400 +++ b/Lib/unittest/mock.py Tue Apr 15 01:39:44 2014 +0530 @@ -2083,6 +2083,10 @@ elif is_type and instance and not _instance_callable(spec): Klass = NonCallableMagicMock + if 'name' in _kwargs: + _name = _kwargs['name'] + del _kwargs['name'] + _new_name = _name if _parent is None: # for a top level object no _new_name should be set diff -r fe532dccf8f6 Lib/unittest/test/testmock/testmock.py --- a/Lib/unittest/test/testmock/testmock.py Mon Apr 14 10:30:43 2014 -0400 +++ b/Lib/unittest/test/testmock/testmock.py Tue Apr 15 01:39:44 2014 +0530 @@ -1164,6 +1164,11 @@ func.mock_calls, [call(1, 2), call(3, 4)] ) + #Issue21222 + def test_create_autospec_withname(self): + def b(): + pass + q = mock.create_autospec(b, name="a") def test_mock_add_spec(self): class _One(object):