https://github.com/python/cpython/commit/34ace5ba4c5764a27a01dc8139232138c8269000
commit: 34ace5ba4c5764a27a01dc8139232138c8269000
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: cjw296 <[email protected]>
date: 2024-06-19T20:53:19Z
summary:

[3.12] gh-120732: Fix `name` passing to `Mock`, when using kwargs to 
`create_autospec` (GH-120737) (#120761)

gh-120732: Fix `name` passing to `Mock`, when using kwargs to `create_autospec` 
(GH-120737)
(cherry picked from commit 1e4815692f6c8a37a3974d0d7d2025494d026d76)

Co-authored-by: Nikita Sobolev <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-06-19-15-06-58.gh-issue-120732.OvYV9b.rst
M Lib/test/test_unittest/testmock/testmock.py
M Lib/unittest/mock.py

diff --git a/Lib/test/test_unittest/testmock/testmock.py 
b/Lib/test/test_unittest/testmock/testmock.py
index 165e2c044d8c5a..1eb1a1bf03a3cc 100644
--- a/Lib/test/test_unittest/testmock/testmock.py
+++ b/Lib/test/test_unittest/testmock/testmock.py
@@ -118,6 +118,11 @@ def 
test_create_autospec_should_be_configurable_by_kwargs(self):
         # pass kwargs with respect to the parent mock.
         self.assertEqual(class_mock().return_value.meth.side_effect, None)
 
+    def test_create_autospec_correctly_handles_name(self):
+        class X: ...
+        mock = create_autospec(X, spec_set=True, name="Y")
+        self.assertEqual(mock._mock_name, "Y")
+
     def test_repr(self):
         mock = Mock(name='foo')
         self.assertIn('foo', repr(mock))
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index ad4b4ebcd42a4f..9398f56506b0aa 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2725,6 +2725,12 @@ def create_autospec(spec, spec_set=False, 
instance=False, _parent=None,
     if not unsafe:
         _check_spec_arg_typos(kwargs)
 
+    _name = kwargs.pop('name', _name)
+    _new_name = _name
+    if _parent is None:
+        # for a top level object no _new_name should be set
+        _new_name = ''
+
     _kwargs.update(kwargs)
 
     Klass = MagicMock
@@ -2742,13 +2748,6 @@ def create_autospec(spec, spec_set=False, 
instance=False, _parent=None,
     elif is_type and instance and not _instance_callable(spec):
         Klass = NonCallableMagicMock
 
-    _name = _kwargs.pop('name', _name)
-
-    _new_name = _name
-    if _parent is None:
-        # for a top level object no _new_name should be set
-        _new_name = ''
-
     mock = Klass(parent=_parent, _new_parent=_parent, _new_name=_new_name,
                  name=_name, **_kwargs)
 
diff --git 
a/Misc/NEWS.d/next/Library/2024-06-19-15-06-58.gh-issue-120732.OvYV9b.rst 
b/Misc/NEWS.d/next/Library/2024-06-19-15-06-58.gh-issue-120732.OvYV9b.rst
new file mode 100644
index 00000000000000..e31c4dd3192d60
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-06-19-15-06-58.gh-issue-120732.OvYV9b.rst
@@ -0,0 +1,2 @@
+Fix ``name`` passing to :class:`unittest.mock.Mock` object when using
+:func:`unittest.mock.create_autospec`.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to