From 15415cc9bbc4fa1e7c1b634cd0ee7b6d9170260e Mon Sep 17 00:00:00 2001 From: Chris Withers Date: Mon, 3 Mar 2025 11:11:17 +0000 Subject: [PATCH 1/2] Mark functions that will never be called with # pragma: no cover --- Lib/test/test_unittest/testmock/testhelpers.py | 2 +- Lib/test/test_unittest/testmock/testmock.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_unittest/testmock/testhelpers.py b/Lib/test/test_unittest/testmock/testhelpers.py index 8d0f3ebc5cba88..d1e48bde982040 100644 --- a/Lib/test/test_unittest/testmock/testhelpers.py +++ b/Lib/test/test_unittest/testmock/testhelpers.py @@ -1080,7 +1080,7 @@ def test_dataclass_with_method(self): class WithMethod: a: int def b(self) -> int: - return 1 + return 1 # pragma: no cover for mock in [ create_autospec(WithMethod, instance=True), diff --git a/Lib/test/test_unittest/testmock/testmock.py b/Lib/test/test_unittest/testmock/testmock.py index 5d1bf4258afacd..386d53bf5a5c63 100644 --- a/Lib/test/test_unittest/testmock/testmock.py +++ b/Lib/test/test_unittest/testmock/testmock.py @@ -316,7 +316,7 @@ def test_explicit_return_value_even_if_mock_wraps_object(self): passed to the wrapped object and the return_value is returned instead. """ def my_func(): - return None + return None # pragma: no cover func_mock = create_autospec(spec=my_func, wraps=my_func) return_value = "explicit return value" func_mock.return_value = return_value From bbf9e871a4591dab09a4a31a3237b47f01febc1b Mon Sep 17 00:00:00 2001 From: Chris Withers Date: Mon, 3 Mar 2025 11:12:58 +0000 Subject: [PATCH 2/2] Fix testpatch.PatchTest.test_exit_idempotent .stop() and __exit__ have subtly different code paths, so to really test __exit__ idempotency, we need to call it specifically twice. --- Lib/test/test_unittest/testmock/testpatch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_unittest/testmock/testpatch.py b/Lib/test/test_unittest/testmock/testpatch.py index 7c5fc3deed2ca2..bd85fdcfc472a6 100644 --- a/Lib/test/test_unittest/testmock/testpatch.py +++ b/Lib/test/test_unittest/testmock/testpatch.py @@ -748,7 +748,7 @@ def test_stop_idempotent(self): def test_exit_idempotent(self): patcher = patch(foo_name, 'bar', 3) with patcher: - patcher.stop() + patcher.__exit__(None, None, None) def test_second_start_failure(self):