Skip to content

Commit d07f7f9

Browse files
committed
serializing reversed lists
1 parent 2d97ea0 commit d07f7f9

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

deepdiff/serialization.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
except ImportError: # pragma: no cover.
4242
PydanticBaseModel = None
4343

44-
from copy import deepcopy
44+
from copy import deepcopy, copy
4545
from functools import partial
4646
from collections.abc import Mapping
4747
from deepdiff.helper import (
@@ -611,6 +611,9 @@ def _convertor(obj):
611611
for original_type, convert_to in _convertor_mapping.items():
612612
if isinstance(obj, original_type):
613613
return convert_to(obj)
614+
# This is to handle reverse() which creates a generator of type list_reverseiterator
615+
if obj.__class__.__name__ == 'list_reverseiterator':
616+
return list(copy(obj))
614617
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)))
615618

616619
return _convertor

docs/diff_doc.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ exclude_paths: list, default = None
4646
:ref:`exclude_paths_label`
4747
List of paths to exclude from the report. If only one item, you can path it as a string.
4848

49-
include_paths: list, default = None
50-
:ref:`include_paths_label`
51-
List of the only paths to include in the report. If only one item, you can path it as a string.
52-
5349
exclude_regex_paths: list, default = None
5450
:ref:`exclude_regex_paths_label`
5551
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.
@@ -67,6 +63,10 @@ exclude_obj_callback_strict: function, default = None
6763
:ref:`exclude_obj_callback_strict_label`
6864
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.
6965

66+
include_paths: list, default = None
67+
:ref:`include_paths_label`
68+
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.
69+
7070
include_obj_callback: function, default = None
7171
:ref:`include_obj_callback_label`
7272
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.

tests/test_serialization.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,13 @@ def test_namedtuple_seriazliation(self):
359359
serialized = json_dumps(op_code)
360360
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}'
361361
assert serialized == expected
362+
363+
def test_reversed_list(self):
364+
items = reversed([1, 2, 3])
365+
366+
serialized = json_dumps(items)
367+
serialized2 = json_dumps(items)
368+
369+
assert '[3,2,1]' == serialized
370+
assert '[3,2,1]' == serialized2, "We should have copied the original list. If this returns empty, it means we exhausted the original list."
371+

0 commit comments

Comments
 (0)