From de270321ac1025218654470ac8e9cca2fcd366de Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Tue, 3 May 2022 01:09:02 -0400 Subject: [PATCH 1/8] fix mocked autospec methods with seal --- Lib/unittest/mock.py | 2 ++ Lib/unittest/test/testmock/testsealable.py | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 8262b7170b76b4b..cdfb46bc8bc525a 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -1181,6 +1181,7 @@ def _execute_mock_call(self, /, *args, **kwargs): if self._mock_wraps is not None: return self._mock_wraps(*args, **kwargs) + # print('self', self) return self.return_value @@ -2731,6 +2732,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None, _new_parent=parent, **kwargs) mock._mock_children[entry] = new + new.return_value = Mock() _check_signature(original, new, skipfirst=skipfirst) # so functions created with _set_signature become instance attributes, diff --git a/Lib/unittest/test/testmock/testsealable.py b/Lib/unittest/test/testmock/testsealable.py index daba2b49b46f635..47f63c2b08e6d8b 100644 --- a/Lib/unittest/test/testmock/testsealable.py +++ b/Lib/unittest/test/testmock/testsealable.py @@ -211,8 +211,6 @@ def ban(self): foo.foo() with self.assertRaises(AttributeError): foo.bar = 1 - with self.assertRaises(AttributeError): - foo.bar2() foo.bar2.return_value = 'bar2' self.assertEqual(foo.bar2(), 'bar2') From 96c516a6195f889ebf372018eb8bca95ea7260bf Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Tue, 3 May 2022 01:15:26 -0400 Subject: [PATCH 2/8] add test --- Lib/unittest/test/testmock/testsealable.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/unittest/test/testmock/testsealable.py b/Lib/unittest/test/testmock/testsealable.py index 47f63c2b08e6d8b..d54d985b6c956e7 100644 --- a/Lib/unittest/test/testmock/testsealable.py +++ b/Lib/unittest/test/testmock/testsealable.py @@ -199,6 +199,7 @@ def ban(self): self.assertIsInstance(foo.Baz, mock.MagicMock) self.assertIsInstance(foo.Baz.baz, mock.NonCallableMagicMock) self.assertIsInstance(foo.Baz.ban, mock.MagicMock) + self.assertIsInstance(foo.bar2(), mock.Mock) self.assertEqual(foo.bar1(), 'a') foo.bar1.return_value = 'new_a' From 0a4a3593388f84a65ae24060277ec8d9e3a09f75 Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Tue, 3 May 2022 01:19:14 -0400 Subject: [PATCH 3/8] remove stray comment --- Lib/unittest/mock.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index cdfb46bc8bc525a..8402e72b0bf2666 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -1181,7 +1181,6 @@ def _execute_mock_call(self, /, *args, **kwargs): if self._mock_wraps is not None: return self._mock_wraps(*args, **kwargs) - # print('self', self) return self.return_value From 8f818492634dc1677fa6f54d916d0c43fa84e4b0 Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Tue, 3 May 2022 11:35:30 -0400 Subject: [PATCH 4/8] add news --- .../next/Library/2022-05-03-11-32-29.gh-issue-91803.pI4Juv.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-05-03-11-32-29.gh-issue-91803.pI4Juv.rst diff --git a/Misc/NEWS.d/next/Library/2022-05-03-11-32-29.gh-issue-91803.pI4Juv.rst b/Misc/NEWS.d/next/Library/2022-05-03-11-32-29.gh-issue-91803.pI4Juv.rst new file mode 100644 index 000000000000000..14829e8fe7777bb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-03-11-32-29.gh-issue-91803.pI4Juv.rst @@ -0,0 +1,3 @@ +Fix an error when using a method of objects mocked with +:func:`unittest.mock.create_autospec` after it was sealed with +:func:`unittest.mock.seal` function. From 6992de757bd660c73619d32abea6ce11371e1092 Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Tue, 3 May 2022 17:16:04 -0400 Subject: [PATCH 5/8] add comment --- Lib/unittest/test/testmock/testsealable.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/unittest/test/testmock/testsealable.py b/Lib/unittest/test/testmock/testsealable.py index d54d985b6c956e7..2ead080672454cd 100644 --- a/Lib/unittest/test/testmock/testsealable.py +++ b/Lib/unittest/test/testmock/testsealable.py @@ -199,6 +199,8 @@ def ban(self): self.assertIsInstance(foo.Baz, mock.MagicMock) self.assertIsInstance(foo.Baz.baz, mock.NonCallableMagicMock) self.assertIsInstance(foo.Baz.ban, mock.MagicMock) + + # see gh-91803 self.assertIsInstance(foo.bar2(), mock.Mock) self.assertEqual(foo.bar1(), 'a') From 36a47d0382e98e597b427a3857abaf2c6c19f6c1 Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Tue, 24 May 2022 14:30:20 -0400 Subject: [PATCH 6/8] use child_klass instead of Mock --- Lib/unittest/mock.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 8402e72b0bf2666..f049432e16844f0 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -2731,7 +2731,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None, _new_parent=parent, **kwargs) mock._mock_children[entry] = new - new.return_value = Mock() + new.return_value = child_klass() _check_signature(original, new, skipfirst=skipfirst) # so functions created with _set_signature become instance attributes, From b1b12ab307237a35099a8134129058f0faf4c0ae Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Sat, 25 Jun 2022 01:15:32 -0400 Subject: [PATCH 7/8] Update Lib/test/test_unittest/testmock/testsealable.py Co-authored-by: Karthikeyan Singaravelan --- Lib/test/test_unittest/testmock/testsealable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_unittest/testmock/testsealable.py b/Lib/test/test_unittest/testmock/testsealable.py index 2ead080672454cd..f188d3f486287d9 100644 --- a/Lib/test/test_unittest/testmock/testsealable.py +++ b/Lib/test/test_unittest/testmock/testsealable.py @@ -201,7 +201,7 @@ def ban(self): self.assertIsInstance(foo.Baz.ban, mock.MagicMock) # see gh-91803 - self.assertIsInstance(foo.bar2(), mock.Mock) + self.assertIsInstance(foo.bar2(), mock.MagicMock) self.assertEqual(foo.bar1(), 'a') foo.bar1.return_value = 'new_a' From c535efb2ce9d9e27443b1b7e0da8b60f8f0bcd4b Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Sun, 3 Jul 2022 14:10:54 -0400 Subject: [PATCH 8/8] expand unittest autospec seal() test --- Lib/test/test_unittest/testmock/testsealable.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_unittest/testmock/testsealable.py b/Lib/test/test_unittest/testmock/testsealable.py index f188d3f486287d9..e0c38293cffd7ac 100644 --- a/Lib/test/test_unittest/testmock/testsealable.py +++ b/Lib/test/test_unittest/testmock/testsealable.py @@ -214,6 +214,8 @@ def ban(self): foo.foo() with self.assertRaises(AttributeError): foo.bar = 1 + with self.assertRaises(AttributeError): + foo.bar2().x foo.bar2.return_value = 'bar2' self.assertEqual(foo.bar2(), 'bar2')