Skip to content

Commit ebc6171

Browse files
committed
1. Created FlatDeltaDict TypedDict in helper.py mirroring FlatDeltaRow's fields, with path and
action required and the rest optional. 2. Changed the return type of to_flat_dicts from List[FlatDeltaRow] to List[FlatDeltaDict]. 3. Used cast to tell pyright the _asdict() output conforms to FlatDeltaDict. 4. Fixed the parameter bug — the original code was hardcoding include_action_in_path=False, report_type_changes=True instead of passing through the actual arguments.
1 parent ffa7029 commit ebc6171

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

LICENSE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
The MIT License (MIT)
22

33
Copyright (c) 2014 - 2026 Sep Dehpour (Seperman) and contributors
4-
www.zepworks.com
4+
qluster.ai
5+
zepworks.com
56

67
Permission is hereby granted, free of charge, to any person obtaining a copy
78
of this software and associated documentation files (the "Software"), to deal

deepdiff/delta.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import copy
22
import logging
3-
from typing import List, Dict, IO, Callable, Set, Union, Optional, Any
3+
from typing import List, Dict, IO, Callable, Set, Union, Optional, Any, cast
44
from functools import partial, cmp_to_key
55
from collections.abc import Mapping
66
from copy import deepcopy
@@ -10,7 +10,7 @@
1010
strings, numbers,
1111
np_ndarray, np_array_factory, numpy_dtypes, get_doc,
1212
not_found, numpy_dtype_string_to_type, dict_,
13-
Opcode, FlatDeltaRow, UnkownValueCode, FlatDataAction,
13+
Opcode, FlatDeltaRow, FlatDeltaDict, UnkownValueCode, FlatDataAction,
1414
OPCODE_TAG_TO_FLAT_DATA_ACTION,
1515
FLAT_DATA_ACTION_TO_OPCODE_TAG,
1616
SetOrdered,
@@ -1065,7 +1065,7 @@ def _from_flat_dicts(flat_dict_list):
10651065

10661066
return result
10671067

1068-
def to_flat_dicts(self, include_action_in_path=False, report_type_changes=True) -> List[FlatDeltaRow]:
1068+
def to_flat_dicts(self, include_action_in_path=False, report_type_changes=True) -> List[FlatDeltaDict]:
10691069
"""
10701070
Returns a flat list of actions that is easily machine readable.
10711071
@@ -1119,9 +1119,9 @@ def to_flat_dicts(self, include_action_in_path=False, report_type_changes=True)
11191119
attribute_added
11201120
attribute_removed
11211121
"""
1122-
return [
1123-
i._asdict() for i in self.to_flat_rows(include_action_in_path=False, report_type_changes=True)
1124-
] # type: ignore
1122+
return cast(List[FlatDeltaDict], [
1123+
i._asdict() for i in self.to_flat_rows(include_action_in_path=include_action_in_path, report_type_changes=report_type_changes)
1124+
])
11251125

11261126
def to_flat_rows(self, include_action_in_path=False, report_type_changes=True) -> List[FlatDeltaRow]:
11271127
"""

deepdiff/helper.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import time
1010
import enum
1111
import ipaddress
12-
from typing import NamedTuple, Any, List, Optional, Dict, Union, TYPE_CHECKING, Tuple, Iterable, Iterator, Set, FrozenSet, Callable, Pattern, Type, TypeVar, Generic, Literal, overload
12+
from typing import NamedTuple, Any, List, Optional, Dict, Union, TYPE_CHECKING, Tuple, Iterable, Iterator, Set, FrozenSet, Callable, Pattern, Type, TypeVar, Generic, Literal, overload, TypedDict
1313
from collections.abc import Mapping, Sequence, Generator
1414
from ast import literal_eval
1515
from decimal import Decimal, localcontext, InvalidOperation as InvalidDecimalOperation
@@ -843,6 +843,23 @@ class FlatDeltaRow(NamedTuple):
843843
__repr__ = __str__ = named_tuple_repr
844844

845845

846+
class _FlatDeltaDictRequired(TypedDict):
847+
path: List
848+
action: FlatDataAction
849+
850+
851+
class FlatDeltaDict(_FlatDeltaDictRequired, total=False):
852+
value: Optional[Any]
853+
old_value: Optional[Any]
854+
type: Optional[Any]
855+
old_type: Optional[Any]
856+
new_path: Optional[List]
857+
t1_from_index: Optional[int]
858+
t1_to_index: Optional[int]
859+
t2_from_index: Optional[int]
860+
t2_to_index: Optional[int]
861+
862+
846863
JSON = Union[Dict[str, str], List[str], List[int], Dict[str, "JSON"], List["JSON"], str, int, float, bool, None]
847864

848865

0 commit comments

Comments
 (0)