python / python/cpython

[docs] Specify that unittest.mock.patch.dict does not make a deepcopy

Open
#144,433 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Documentation

I always assumed that unittest.mock.patch.dict made a recursive (deep) copy of the dict and restored it afterwards.
It turns out it is a "simple" copy()

We should maybe clarify that in the documentation

Example:


foo = {
  "bar": {"baz": 1}
}

with patch.dict(foo, {"unrelated": 123}):
  foo["bar"]["baz"] = 2

# baz will stay at 2, assumed it was reset to the original 1
Linked PRs
  • gh-144434

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the unittest.mock.patch.dict documentation and review the existing explanation of how the dictionary is restored. Clarify that it uses a shallow copy rather than a recursive deepcopy, using the nested-dictionary example from the issue to show that inner changes remain. Check the documentation build after editing.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.