summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/unittest/mock.py6
-rw-r--r--Lib/unittest/test/testmock/testpatch.py9
2 files changed, 10 insertions, 5 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 6226bd4bc0..7152f86ed9 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1602,9 +1602,9 @@ class _patch(object):
def _get_target(target):
try:
target, attribute = target.rsplit('.', 1)
- except (TypeError, ValueError):
- raise TypeError("Need a valid target to patch. You supplied: %r" %
- (target,))
+ except (TypeError, ValueError, AttributeError):
+ raise TypeError(
+ f"Need a valid target to patch. You supplied: {target!r}")
getter = lambda: _importer(target)
return getter, attribute
diff --git a/Lib/unittest/test/testmock/testpatch.py b/Lib/unittest/test/testmock/testpatch.py
index 233a5afffa..8ab63a1317 100644
--- a/Lib/unittest/test/testmock/testpatch.py
+++ b/Lib/unittest/test/testmock/testpatch.py
@@ -1933,8 +1933,13 @@ class PatchTest(unittest.TestCase):
def test_invalid_target(self):
- with self.assertRaises(TypeError):
- patch('')
+ class Foo:
+ pass
+
+ for target in ['', 12, Foo()]:
+ with self.subTest(target=target):
+ with self.assertRaises(TypeError):
+ patch(target)
def test_cant_set_kwargs_when_passing_a_mock(self):