top of page

A Copied Dictionary Can Still Share the Nested Data You Change

Oct 26
3 min read

A preview function copies a request dictionary, edits a nested list and produces the expected draft. Later, the original request has changed too. The developer can point to the copy operation, yet the user’s supposedly untouched input is no longer intact.


This is a common ownership mistake when compound objects contain other mutable objects. The question is not whether a copy happened at all. It is whether the objects that will be modified were actually separated.


Distinguish a new container from independent contents


Python’s copy documentation explains that a shallow copy creates a new outer compound object while retaining references to its contents. A deep copy recursively copies contained objects, subject to type-specific behavior. Assignment alone does not copy an object.


For a business payload, identify the exact nested values the operation will change. A new outer dictionary may be enough when only a top-level value is replaced. It is not sufficient evidence of isolation when the code mutates a list or dictionary still shared with the original.


Draw the ownership boundary before choosing a repair


List the data that belongs to the caller and the data the function may modify. Include nested collections and any objects supplied by external libraries. Decide whether the output is a temporary working view or an independent result that must remain stable.


Avoid making deep copy the automatic answer to every ownership issue. The documentation notes that it can copy more than intended and does not copy every kind of object. A carefully constructed output can make the allowed fields and ownership clearer than copying an entire runtime object graph.


Illustrative example: preview tags alter the source request


Imagine a request with a nested list of labels. A preview adds a temporary “needs-review” label after making a shallow copy of the request dictionary. If both dictionaries still refer to the same label list, the original request can receive that temporary label too.


The meaningful test compares the original request before and after preview generation. Merely checking that the two outer dictionaries are different objects misses the nested sharing that causes the problem.


Illustrative example: a top-level replacement is different


In another case, the preview replaces a top-level status string in the copied dictionary. That operation can leave the original dictionary’s status unchanged. This does not prove that every later nested edit will also be isolated.


Write separate examples for replacing a value and mutating a contained object. The distinction helps reviewers assess the actual change path instead of relying on the reassuring presence of a copy call.


Choose the narrowest clear construction


For a simple data transfer object, consider constructing the preview explicitly from allowed fields and creating fresh mutable collections where needed. This makes it easier to exclude internal handles, cached values or fields that should never reach the preview.


If a deep copy is appropriate, test it on the actual supported object types and realistic payload size. State which identities should remain shared and which should not. Do not claim that deep copying a request also creates an independent database record or duplicates an external service resource.


Keep the source object available for a comparison during review. A snapshot of its relevant values can help identify unexpected mutation, but the snapshot itself must not share the mutable values being tested.


Test both directions of accidental influence


After creating the preview, modify the preview’s nested collection and check the source. Then modify a fresh source instance and check the preview if the contract promises a stable snapshot. This tests the business expectation more directly than inspecting one object identifier.


Include an empty nested list, several entries and a nested dictionary. Use distinguishable synthetic values so a failure clearly identifies which boundary was shared. Check the function’s documented mutation contract as well as the returned content.


For operations intended to be non-destructive, connect this check to the bulk-change dry-run review. A preview should describe a proposed change without quietly applying part of that change to its input.


Editorial note


Prepared with AI assistance. Primary sources and the FutureOS article inventory were checked on October 26, 2026. Examples are illustrative, not customer results. FutureOS is described as being in prelaunch in its current introduction. This guide does not claim an available product feature, integration or service, and no independent expert review is claimed.


Comments


Commenting on this post isn't available anymore. Contact the site owner for more info.
bottom of page