MCPcopy Index your code
hub / github.com/RustPython/RustPython / patch

Function patch

Lib/unittest/mock.py:1764–1843  ·  view source on GitHub ↗

`patch` acts as a function decorator, class decorator or a context manager. Inside the body of the function or with statement, the `target` is patched with a `new` object. When the function/with statement exits the patch is undone. If `new` is omitted, then the target is replac

(
        target, new=DEFAULT, spec=None, create=False,
        spec_set=None, autospec=None, new_callable=None, *, unsafe=False, **kwargs
    )

Source from the content-addressed store, hash-verified

1762
1763
1764def patch(
1765 target, new=DEFAULT, spec=None, create=False,
1766 spec_set=None, autospec=None, new_callable=None, *, unsafe=False, **kwargs
1767 ):
1768 """
1769 `patch` acts as a function decorator, class decorator or a context
1770 manager. Inside the body of the function or with statement, the `target`
1771 is patched with a `new` object. When the function/with statement exits
1772 the patch is undone.
1773
1774 If `new` is omitted, then the target is replaced with an
1775 `AsyncMock` if the patched object is an async function or a
1776 `MagicMock` otherwise. If `patch` is used as a decorator and `new` is
1777 omitted, the created mock is passed in as an extra argument to the
1778 decorated function. If `patch` is used as a context manager the created
1779 mock is returned by the context manager.
1780
1781 `target` should be a string in the form `'package.module.ClassName'`. The
1782 `target` is imported and the specified object replaced with the `new`
1783 object, so the `target` must be importable from the environment you are
1784 calling `patch` from. The target is imported when the decorated function
1785 is executed, not at decoration time.
1786
1787 The `spec` and `spec_set` keyword arguments are passed to the `MagicMock`
1788 if patch is creating one for you.
1789
1790 In addition you can pass `spec=True` or `spec_set=True`, which causes
1791 patch to pass in the object being mocked as the spec/spec_set object.
1792
1793 `new_callable` allows you to specify a different class, or callable object,
1794 that will be called to create the `new` object. By default `AsyncMock` is
1795 used for async functions and `MagicMock` for the rest.
1796
1797 A more powerful form of `spec` is `autospec`. If you set `autospec=True`
1798 then the mock will be created with a spec from the object being replaced.
1799 All attributes of the mock will also have the spec of the corresponding
1800 attribute of the object being replaced. Methods and functions being
1801 mocked will have their arguments checked and will raise a `TypeError` if
1802 they are called with the wrong signature. For mocks replacing a class,
1803 their return value (the 'instance') will have the same spec as the class.
1804
1805 Instead of `autospec=True` you can pass `autospec=some_object` to use an
1806 arbitrary object as the spec instead of the one being replaced.
1807
1808 By default `patch` will fail to replace attributes that don't exist. If
1809 you pass in `create=True`, and the attribute doesn't exist, patch will
1810 create the attribute for you when the patched function is called, and
1811 delete it again afterwards. This is useful for writing tests against
1812 attributes that your production code creates at runtime. It is off by
1813 default because it can be dangerous. With it switched on you can write
1814 passing tests against APIs that don't actually exist!
1815
1816 Patch can be used as a `TestCase` class decorator. It works by
1817 decorating each test method in the class. This reduces the boilerplate
1818 code when your test methods share a common patchings set. `patch` finds
1819 tests by looking for method names that start with `patch.TEST_PREFIX`.
1820 By default this is `test`, which matches the way `unittest` finds tests.
1821 You can specify an alternative prefix by setting `patch.TEST_PREFIX`.

Calls 2

_get_targetFunction · 0.85
_patchClass · 0.70