refactor(typing): take a Mapping in delete_nested_value and cover the fresh copy

This commit is contained in:
mateo-berri 2026-10-23 18:52:25 -07:00
parent 0e77a203e8
commit 9432192d68
2 changed files with 12 additions and 2 deletions

View file

@ -181,7 +181,7 @@ def _delete_nested_value_custom(
def delete_nested_value(
data: dict[str, object],
data: Mapping[str, object],
path: str,
depth: int = 0,
max_depth: int = 20,
@ -214,7 +214,8 @@ def delete_nested_value(
"""
import copy
result: Final = copy.deepcopy(data)
copied: Final = copy.deepcopy(data)
result: Final = copied if isinstance(copied, dict) else dict(copied)
try:
# Parse path into segments

View file

@ -2,6 +2,8 @@
Tests for litellm.litellm_core_utils.dot_notation_indexing module.
"""
from typing import Final
import pytest
from litellm.litellm_core_utils.dot_notation_indexing import (
@ -125,3 +127,10 @@ class TestDeleteNestedValue:
data = {"items": [{"a": 1, "b": 2}, {"a": 3, "b": 4}]}
result = delete_nested_value(data, "items[0].b")
assert result == {"items": [{"a": 1}, {"a": 3, "b": 4}]}
def test_delete_returns_a_fresh_dict_and_leaves_a_narrowly_typed_input_untouched(self):
payload: Final[dict[str, list[str]]] = {"tags": ["a", "b"], "secret": ["s"]}
result = delete_nested_value(payload, "secret")
assert result == {"tags": ["a", "b"]}
assert payload == {"tags": ["a", "b"], "secret": ["s"]}
assert result["tags"] is not payload["tags"]