fix: Improve type safety in dot_notation_indexing for nested field removal

- Change _delete_nested_value_custom parameter type from Dict[str, Any] to Union[Dict[str, Any], List[Any]] to accurately reflect that it handles both dicts and lists
- Add explicit isinstance checks before recursing into list items to ensure type safety
- Prevents potential runtime errors when encountering primitive types in nested structures
This commit is contained in:
Sameer Kankute
2025-12-09 17:44:24 +05:30
parent 9bb0e7dd75
commit 4ff992a073
2 changed files with 2054 additions and 5 deletions
@@ -22,7 +22,7 @@ Used by JWT Auth to get the user role from the token, and by
additional_drop_params to remove nested fields from optional parameters.
"""
from typing import Any, Dict, Optional, TypeVar
from typing import Any, Dict, List, Optional, TypeVar, Union
T = TypeVar("T")
@@ -106,7 +106,7 @@ def _parse_path_segments(path: str) -> list:
def _delete_nested_value_custom(
data: Dict[str, Any],
data: Union[Dict[str, Any], List[Any]],
segments: list,
segment_index: int = 0,
) -> None:
@@ -134,7 +134,9 @@ def _delete_nested_value_custom(
# Can't delete array elements themselves, skip
pass
else:
_delete_nested_value_custom(item, segments, segment_index + 1)
# Only recurse if item is a dict or list (nested structure)
if isinstance(item, (dict, list)):
_delete_nested_value_custom(item, segments, segment_index + 1)
return
# Handle array index: [0], [1], [2], etc.
@@ -146,7 +148,10 @@ def _delete_nested_value_custom(
# Can't delete array elements themselves, skip
pass
else:
_delete_nested_value_custom(data[index], segments, segment_index + 1)
# Only recurse if element is a dict or list (nested structure)
element = data[index]
if isinstance(element, (dict, list)):
_delete_nested_value_custom(element, segments, segment_index + 1)
except (ValueError, IndexError):
# Invalid index, skip
pass
File diff suppressed because it is too large Load Diff