(item: _T, paths: Sequence[Sequence[str]], index: int)
| 146 | |
| 147 | |
| 148 | def _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 |
no test coverage detected