From 687bd3f50e6cee016f21d2e3bd1cf66242ee95f7 Mon Sep 17 00:00:00 2001 From: Elena O <31424287+oklena@users.noreply.github.com> Date: Sun, 9 Nov 2025 14:57:08 -0800 Subject: [PATCH 1/3] rewrote Mocking Unbound Methods paragraph to second person --- Doc/library/unittest.mock-examples.rst | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/Doc/library/unittest.mock-examples.rst b/Doc/library/unittest.mock-examples.rst index 00cc9bfc0a5f2b..47f3eeeece1572 100644 --- a/Doc/library/unittest.mock-examples.rst +++ b/Doc/library/unittest.mock-examples.rst @@ -743,16 +743,15 @@ exception is raised in the setUp then tearDown is not called. Mocking Unbound Methods ~~~~~~~~~~~~~~~~~~~~~~~ -Whilst writing tests today I needed to patch an *unbound method* (patching the -method on the class rather than on the instance). I needed self to be passed -in as the first argument because I want to make asserts about which objects -were calling this particular method. The issue is that you can't patch with a -mock for this, because if you replace an unbound method with a mock it doesn't -become a bound method when fetched from the instance, and so it doesn't get -self passed in. The workaround is to patch the unbound method with a real -function instead. The :func:`patch` decorator makes it so simple to -patch out methods with a mock that having to create a real function becomes a -nuisance. +Sometimes a test needs to patch an *unbound method* which means patching the +method on the class rather than on the instance. In order to make asserts +about which objects were calling this particular method, you need to pass +self as the first argument. The issue is that you can't patch with a mock for +this, because if you replace an unbound method with a mock it doesn't become +a bound method when fetched from the instance, and so it doesn't get self +passed in. The workaround is to patch the unbound method with a real function +instead. The :func:`patch` decorator makes it so simple to patch out methods +with a mock that having to create a real function becomes a nuisance. If you pass ``autospec=True`` to patch then it does the patching with a *real* function object. This function object has the same signature as the one @@ -760,8 +759,8 @@ it is replacing, but delegates to a mock under the hood. You still get your mock auto-created in exactly the same way as before. What it means though, is that if you use it to patch out an unbound method on a class the mocked function will be turned into a bound method if it is fetched from an instance. -It will have ``self`` passed in as the first argument, which is exactly what I -wanted: +It will have ``self`` passed in as the first argument, which is exactly what +was needed: >>> class Foo: ... def foo(self): From 5cf5edc73718555c3e2ddf1d21c4717ef5dc23fd Mon Sep 17 00:00:00 2001 From: Elena O <31424287+oklena@users.noreply.github.com> Date: Sun, 9 Nov 2025 15:28:11 -0800 Subject: [PATCH 2/3] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: C.A.M. Gerlach Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Doc/library/unittest.mock-examples.rst | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Doc/library/unittest.mock-examples.rst b/Doc/library/unittest.mock-examples.rst index 47f3eeeece1572..bf4a4f7a274a1e 100644 --- a/Doc/library/unittest.mock-examples.rst +++ b/Doc/library/unittest.mock-examples.rst @@ -743,14 +743,14 @@ exception is raised in the setUp then tearDown is not called. Mocking Unbound Methods ~~~~~~~~~~~~~~~~~~~~~~~ -Sometimes a test needs to patch an *unbound method* which means patching the -method on the class rather than on the instance. In order to make asserts -about which objects were calling this particular method, you need to pass -self as the first argument. The issue is that you can't patch with a mock for +Sometimes a test needs to patch an *unbound method*, which means patching the +method on the class rather than on the instance. In order to make assertions +about which objects were calling this particular method, you need to pass +``self`` as the first argument. The issue is that you can't patch with a mock for this, because if you replace an unbound method with a mock it doesn't become -a bound method when fetched from the instance, and so it doesn't get self +a bound method when fetched from the instance, and so it doesn't get ``self`` passed in. The workaround is to patch the unbound method with a real function -instead. The :func:`patch` decorator makes it so simple to patch out methods +instead. The :func:`patch` decorator makes it so simple to patch out methods with a mock that having to create a real function becomes a nuisance. If you pass ``autospec=True`` to patch then it does the patching with a @@ -759,7 +759,7 @@ it is replacing, but delegates to a mock under the hood. You still get your mock auto-created in exactly the same way as before. What it means though, is that if you use it to patch out an unbound method on a class the mocked function will be turned into a bound method if it is fetched from an instance. -It will have ``self`` passed in as the first argument, which is exactly what +It will have ``self`` passed in as the first argument, which is exactly what was needed: >>> class Foo: From 5a3b84d11fae0ddd04b431bd4aaae6974019fa53 Mon Sep 17 00:00:00 2001 From: Elena O <31424287+oklena@users.noreply.github.com> Date: Sun, 9 Nov 2025 15:31:11 -0800 Subject: [PATCH 3/3] removed whitespace Co-authored-by: C.A.M. Gerlach --- Doc/library/unittest.mock-examples.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/unittest.mock-examples.rst b/Doc/library/unittest.mock-examples.rst index bf4a4f7a274a1e..e2b0322dae0407 100644 --- a/Doc/library/unittest.mock-examples.rst +++ b/Doc/library/unittest.mock-examples.rst @@ -747,7 +747,7 @@ Sometimes a test needs to patch an *unbound method*, which means patching the method on the class rather than on the instance. In order to make assertions about which objects were calling this particular method, you need to pass ``self`` as the first argument. The issue is that you can't patch with a mock for -this, because if you replace an unbound method with a mock it doesn't become +this, because if you replace an unbound method with a mock it doesn't become a bound method when fetched from the instance, and so it doesn't get ``self`` passed in. The workaround is to patch the unbound method with a real function instead. The :func:`patch` decorator makes it so simple to patch out methods