mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
bpo-41403: Improve error message for invalid mock target (GH-30833) (GH-30835)
(cherry picked from commit f7955a82e3)
This commit is contained in:
parent
94d6434ba7
commit
1398dca838
3 changed files with 13 additions and 5 deletions
|
|
@ -1557,9 +1557,9 @@ def stop(self):
|
||||||
def _get_target(target):
|
def _get_target(target):
|
||||||
try:
|
try:
|
||||||
target, attribute = target.rsplit('.', 1)
|
target, attribute = target.rsplit('.', 1)
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError, AttributeError):
|
||||||
raise TypeError("Need a valid target to patch. You supplied: %r" %
|
raise TypeError(
|
||||||
(target,))
|
f"Need a valid target to patch. You supplied: {target!r}")
|
||||||
getter = lambda: _importer(target)
|
getter = lambda: _importer(target)
|
||||||
return getter, attribute
|
return getter, attribute
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1933,8 +1933,13 @@ def test(mock):
|
||||||
|
|
||||||
|
|
||||||
def test_invalid_target(self):
|
def test_invalid_target(self):
|
||||||
with self.assertRaises(TypeError):
|
class Foo:
|
||||||
patch('')
|
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):
|
def test_cant_set_kwargs_when_passing_a_mock(self):
|
||||||
|
|
|
||||||
|
|
@ -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.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue