bpo-38669: patch.object now raises a helpful error (GH17510)
This means a clearer message is now shown when patch.object is called with two string arguments, rather than a class and a string argument.
(cherry picked from commit cd90a52983db34896a6335a572d55bdda274778f)
Co-authored-by: Elena Oat <oat.elena@gmail.com>
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index d6e3067..e92ccf1 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1587,6 +1587,10 @@
When used as a class decorator `patch.object` honours `patch.TEST_PREFIX`
for choosing which methods to wrap.
"""
+ if type(target) is str:
+ raise TypeError(
+ f"{target!r} must be the actual object to be patched, not a str"
+ )
getter = lambda: target
return _patch(
getter, attribute, new, spec, create,