MCPcopy
hub / github.com/openai/openai-python / _deepcopy_with_paths

Function _deepcopy_with_paths

src/openai/_files.py:148–173  ·  view source on GitHub ↗
(item: _T, paths: Sequence[Sequence[str]], index: int)

Source from the content-addressed store, hash-verified

146
147
148def _deepcopy_with_paths(item: _T, paths: Sequence[Sequence[str]], index: int) -> _T:
149 if not paths:
150 return item
151 if is_mapping(item):
152 key_to_paths: dict[str, list[Sequence[str]]] = {}
153 for path in paths:
154 if index < len(path):
155 key_to_paths.setdefault(path[index], []).append(path)
156
157 # if no path continues through this mapping, it won't be mutated and copying it is redundant
158 if not key_to_paths:
159 return item
160
161 result = dict(item)
162 for key, subpaths in key_to_paths.items():
163 if key in result:
164 result[key] = _deepcopy_with_paths(result[key], subpaths, index + 1)
165 return cast(_T, result)
166 if is_list(item):
167 array_paths = [path for path in paths if index < len(path) and path[index] == "<array>"]
168
169 # if no path expects a list here, nothing will be mutated inside it - return by reference
170 if not array_paths:
171 return cast(_T, item)
172 return cast(_T, [_deepcopy_with_paths(entry, array_paths, index + 1) for entry in item])
173 return item

Callers 1

deepcopy_with_pathsFunction · 0.85

Calls 4

is_mappingFunction · 0.85
is_listFunction · 0.85
appendMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected