File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ import sys
12from collections import OrderedDict
23from collections .abc import Mapping , MutableMapping
34
@@ -28,6 +29,22 @@ def __reduce__(self):
2829 # but preserve the raw data.
2930 return (dict , (dict (self ),))
3031
32+ if sys .version_info >= (3 , 9 ):
33+ # These are basically copied from OrderedDict, with `serializer` added.
34+ def __or__ (self , other ):
35+ if not isinstance (other , dict ):
36+ return NotImplemented
37+ new = self .__class__ (self , serializer = self .serializer )
38+ new .update (other )
39+ return new
40+
41+ def __ror__ (self , other ):
42+ if not isinstance (other , dict ):
43+ return NotImplemented
44+ new = self .__class__ (other , serializer = self .serializer )
45+ new .update (self )
46+ return new
47+
3148
3249class ReturnList (list ):
3350 """
Original file line number Diff line number Diff line change @@ -740,3 +740,25 @@ class TestSerializer(A, B):
740740 'f4' : serializers .CharField ,
741741 'f5' : serializers .CharField ,
742742 }
743+
744+
745+ class Test8301Regression :
746+ @pytest .mark .skipif (
747+ sys .version_info < (3 , 9 ),
748+ reason = "dictionary union operator requires Python 3.9 or higher" ,
749+ )
750+ def test_ReturnDict_merging (self ):
751+ # Serializer.data returns ReturnDict, this is essentially a test for that.
752+
753+ class TestSerializer (serializers .Serializer ):
754+ char = serializers .CharField ()
755+
756+ s = TestSerializer (data = {'char' : 'x' })
757+ assert s .is_valid ()
758+ assert s .data | {} == {'char' : 'x' }
759+ assert s .data | {'other' : 'y' } == {'char' : 'x' , 'other' : 'y' }
760+ assert {} | s .data == {'char' : 'x' }
761+ assert {'other' : 'y' } | s .data == {'char' : 'x' , 'other' : 'y' }
762+
763+ assert (s .data | {}).__class__ == s .data .__class__
764+ assert ({} | s .data ).__class__ == s .data .__class__
You can’t perform that action at this time.
0 commit comments