Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1602,9 +1602,9 @@ def stop(self):
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

Expand Down
9 changes: 7 additions & 2 deletions Lib/unittest/test/testmock/testpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1933,8 +1933,13 @@ def test(mock):


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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Make :meth:`mock.patch` raise a :exc:`TypeError` with a relevant error
message on invalid arg. Previously it allowed a cryptic
:exc:`AttributeError` to escape.