From 1bc3518ebc846e182f079fe66619af7e7f99945b Mon Sep 17 00:00:00 2001 From: Idan Weiss Date: Wed, 2 Dec 2020 09:33:17 +0200 Subject: [PATCH 1/6] Check if NonCallableMock's spec_arg is not None instead of call its __bool__ function --- 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 f5f502f257244c..4db1bacf4b10cf 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -406,7 +406,7 @@ def __new__(cls, /, *args, **kw): # Check if spec is an async object or function bound_args = _MOCK_SIG.bind_partial(cls, *args, **kw).arguments spec_arg = bound_args.get('spec_set', bound_args.get('spec')) - if spec_arg and _is_async_obj(spec_arg): + if spec_arg is not None and _is_async_obj(spec_arg): bases = (AsyncMockMixin, cls) new = type(cls.__name__, bases, {'__doc__': cls.__doc__}) instance = _safe_super(NonCallableMock, cls).__new__(new) From 81e35b91988d98d1ba49c62f67f06c20b33f76cd Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 2 Dec 2020 07:38:00 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2020-12-02-07-37-59.bpo-42532.ObNep_.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-12-02-07-37-59.bpo-42532.ObNep_.rst diff --git a/Misc/NEWS.d/next/Library/2020-12-02-07-37-59.bpo-42532.ObNep_.rst b/Misc/NEWS.d/next/Library/2020-12-02-07-37-59.bpo-42532.ObNep_.rst new file mode 100644 index 00000000000000..0388961fa24199 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-12-02-07-37-59.bpo-42532.ObNep_.rst @@ -0,0 +1 @@ +Check if NonCallableMock.__new__'s spec_arg is not None instead of calling its __bool__ function \ No newline at end of file From d3b40aa3c67c04cdc635593849b171102d5329ac Mon Sep 17 00:00:00 2001 From: idanw206 <31290383+idanw206@users.noreply.github.com> Date: Fri, 4 Dec 2020 18:03:47 +0200 Subject: [PATCH 3/6] Update Misc/NEWS.d/next/Library/2020-12-02-07-37-59.bpo-42532.ObNep_.rst Co-authored-by: Mario Corchero --- .../next/Library/2020-12-02-07-37-59.bpo-42532.ObNep_.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2020-12-02-07-37-59.bpo-42532.ObNep_.rst b/Misc/NEWS.d/next/Library/2020-12-02-07-37-59.bpo-42532.ObNep_.rst index 0388961fa24199..7465cb8e2e3d7b 100644 --- a/Misc/NEWS.d/next/Library/2020-12-02-07-37-59.bpo-42532.ObNep_.rst +++ b/Misc/NEWS.d/next/Library/2020-12-02-07-37-59.bpo-42532.ObNep_.rst @@ -1 +1 @@ -Check if NonCallableMock.__new__'s spec_arg is not None instead of calling its __bool__ function \ No newline at end of file +Remove unexpected call of ``__bool__`` when passing a ``spec_arg`` argument to a Mock. From 71c4f0cb7c377664bdff7ece8a5c8c67c7411dc0 Mon Sep 17 00:00:00 2001 From: Idan Weiss Date: Sat, 5 Dec 2020 19:47:46 +0200 Subject: [PATCH 4/6] Added test for unexpected call of when passing spec_arg in mock module --- Lib/unittest/test/testmock/testmock.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 194ce3f61bbfdd..85782b0e363fd0 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -2164,6 +2164,16 @@ def trace(frame, event, arg): # pragma: no cover for mock in mocks: obj = mock(spec=Something) self.assertIsInstance(obj, Something) + + def test_bool_not_called_when_passing_spec_arg(self): + class Something: + def __init__(self): + self.obj_with_bool_func = unittest.mock.MagicMock() + + obj = Something() + with unittest.mock.patch.object(obj, 'obj_with_bool_func', autospec=True): + pass + self.assertEqual(obj.obj_with_bool_func.__bool__.call_count, 0) if __name__ == '__main__': From 190c9ed8aa3a0f381b16932e2d72ef179e727c9b Mon Sep 17 00:00:00 2001 From: Idan Weiss Date: Sun, 6 Dec 2020 11:19:09 +0200 Subject: [PATCH 5/6] Fixed whitespaces in testmock --- Lib/unittest/test/testmock/testmock.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 85782b0e363fd0..089a0a11bb9453 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -2171,8 +2171,8 @@ def __init__(self): self.obj_with_bool_func = unittest.mock.MagicMock() obj = Something() - with unittest.mock.patch.object(obj, 'obj_with_bool_func', autospec=True): - pass + with unittest.mock.patch.object(obj, 'obj_with_bool_func', autospec=True): pass + self.assertEqual(obj.obj_with_bool_func.__bool__.call_count, 0) From b853185fba67b23c471952ff74d31b274da731c0 Mon Sep 17 00:00:00 2001 From: Idan Weiss Date: Sun, 6 Dec 2020 11:27:45 +0200 Subject: [PATCH 6/6] Fixed whitespaces in testmock.py --- Lib/unittest/test/testmock/testmock.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 089a0a11bb9453..dfcf1ef2ee0302 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -2164,7 +2164,7 @@ def trace(frame, event, arg): # pragma: no cover for mock in mocks: obj = mock(spec=Something) self.assertIsInstance(obj, Something) - + def test_bool_not_called_when_passing_spec_arg(self): class Something: def __init__(self):