Skip to content

Commit

Permalink
serializing reversed lists
Browse files Browse the repository at this point in the history
  • Loading branch information
seperman committed Jun 18, 2024
1 parent 2d97ea0 commit d07f7f9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
5 changes: 4 additions & 1 deletion deepdiff/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
except ImportError: # pragma: no cover.
PydanticBaseModel = None

from copy import deepcopy
from copy import deepcopy, copy
from functools import partial
from collections.abc import Mapping
from deepdiff.helper import (
Expand Down Expand Up @@ -611,6 +611,9 @@ def _convertor(obj):
for original_type, convert_to in _convertor_mapping.items():
if isinstance(obj, original_type):
return convert_to(obj)
# This is to handle reverse() which creates a generator of type list_reverseiterator
if obj.__class__.__name__ == 'list_reverseiterator':
return list(copy(obj))
raise TypeError('We do not know how to convert {} of type {} for json serialization. Please pass the default_mapping parameter with proper mapping of the object to a basic python type.'.format(obj, type(obj)))

return _convertor
Expand Down
8 changes: 4 additions & 4 deletions docs/diff_doc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ exclude_paths: list, default = None
:ref:`exclude_paths_label`
List of paths to exclude from the report. If only one item, you can path it as a string.

include_paths: list, default = None
:ref:`include_paths_label`
List of the only paths to include in the report. If only one item, you can path it as a string.

exclude_regex_paths: list, default = None
:ref:`exclude_regex_paths_label`
List of string regex paths or compiled regex paths objects to exclude from the report. If only one item, you can pass it as a string or regex compiled object.
Expand All @@ -67,6 +63,10 @@ exclude_obj_callback_strict: function, default = None
:ref:`exclude_obj_callback_strict_label`
A function that works the same way as exclude_obj_callback, but excludes elements from the result only if the function returns True for both elements.

include_paths: list, default = None
:ref:`include_paths_label`
List of the only paths to include in the report. If only one item is in the list, you can pass it as a string.

include_obj_callback: function, default = None
:ref:`include_obj_callback_label`
A function that takes the object and its path and returns a Boolean. If True is returned, the object is included in the results, otherwise it is excluded.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,13 @@ def test_namedtuple_seriazliation(self):
serialized = json_dumps(op_code)
expected = '{"tag":"replace","t1_from_index":0,"t1_to_index":1,"t2_from_index":10,"t2_to_index":20,"old_values":null,"new_values":null}'
assert serialized == expected

def test_reversed_list(self):
items = reversed([1, 2, 3])

serialized = json_dumps(items)
serialized2 = json_dumps(items)

assert '[3,2,1]' == serialized
assert '[3,2,1]' == serialized2, "We should have copied the original list. If this returns empty, it means we exhausted the original list."

0 comments on commit d07f7f9

Please sign in to comment.