Skip to content
Open
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
1 change: 1 addition & 0 deletions changelog/11022.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Document safer alternatives and scope guidance for monkeypatching standard library functions.
9 changes: 8 additions & 1 deletion doc/en/how-to/monkeypatch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,14 @@ so that any attempts within tests to create http requests will fail.
.. note::

Mind that patching ``stdlib`` functions and some third-party libraries used by pytest
might break pytest itself, therefore in those cases it is recommended to use
might break pytest itself. Prefer patching the reference that your code uses
instead of patching the original object in the standard library. For example,
if your module does ``from os import getcwd``, patch ``mymodule.getcwd``
rather than ``os.getcwd``.

For code that you control, a safer long-term pattern is to make dependencies
explicit so they can be passed into the code under test instead of patched
globally. When patching a stdlib object is unavoidable, use
:meth:`MonkeyPatch.context` to limit the patching to the block you want tested:

.. code-block:: python
Expand Down
Loading