One function

_patch_object in testing-cabal/mock

The author described this change as bpo-38669: patch.object now raises a helpful error (GH17034). It counts as a record because the check below fails on the code as it stood at a6768e641 and passes on c000fff5b, with nothing else changed between the two runs.

Fix saved2020-01-29
Sharing licenceBSD-2-Clause · LICENSE.txt
Change size+4 0

What the code was meant to do, written into the code itself as a docstring

patch the named member (`attribute`) on an object (`target`) with a mock object. `patch.object` can be used as a decorator, class decorator or a context manager. Arguments `new`, `spec`, `create`, `spec_set`, `autospec` and `new_callable` have the same meaning as for `patch`. Like `patch`, `patch.object` takes arbitrary keyword arguments for configuring the mock object it creates. When used as a class decorator `patch.object` honours `patch.TEST_PREFIX` for choosing which methods to wrap.

The change

1616 When used as a class decorator `patch.object` honours `patch.TEST_PREFIX`
1717 for choosing which methods to wrap.
1818 """
19+ if type(target) is str:
20+ raise TypeError(
21+ f"{target!r} must be the actual object to be patched, not a str"
22+ )
1923 getter = lambda: target
2024 return _patch(
2125 getter, attribute, new, spec, create,

The check that tells the two apart

failpass·mock/tests/testpatch.py::PatchTest::test_patchobject_with_string_as_target

Check file mock/tests/testpatch.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before ita6768e64120cd6c963035044e9d70fb85f2dabc0
Broken version dated2020-01-29
Modulemock.mock
Units changed_patch_object
Fingerprinte8da7c498902bdad
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in testing-cabal/mock