Skip to content

Commit ffe3e4f

Browse files
authored
extend the Encoder/Decoder state API to be type-aware (#13017)
1 parent 38aecc7 commit ffe3e4f

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

localstack-core/localstack/state/core.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,42 +97,48 @@ def __str__(self) -> str:
9797

9898

9999
class Encoder:
100-
def encodes(self, obj: Any) -> bytes:
100+
def encodes(self, obj: Any, py_type: type = None) -> bytes:
101101
"""
102102
Encode an object into bytes.
103103
104104
:param obj: the object to encode
105+
:param py_type: the type of the object. needed by some encoders that don't have implicit type knowledge.
105106
:return: the encoded object
106107
"""
107108
b = io.BytesIO()
108109
self.encode(obj, b)
109110
return b.getvalue()
110111

111-
def encode(self, obj: Any, file: IO[bytes]):
112+
def encode(self, obj: Any, file: IO[bytes], py_type: type = None):
112113
"""
113114
Encode an object into bytes.
114115
115116
:param obj: the object to encode
117+
:param py_type: the type of the object. needed by some encoders that don't have implicit type knowledge.
116118
:param file: the file to write the encoded data into
117119
"""
118120
raise NotImplementedError
119121

120122

121123
class Decoder:
122-
def decodes(self, data: bytes) -> Any:
124+
def decodes(self, data: bytes, py_type: type = None) -> Any:
123125
"""
124126
Decode a previously encoded object.
125127
126128
:param data: the encoded object to decode
129+
:param py_type: the type that is expected as return type. Needed by some decoders that don't have implicit
130+
type knowledge.
127131
:return: the decoded object
128132
"""
129-
return self.decode(io.BytesIO(data))
133+
return self.decode(io.BytesIO(data), py_type)
130134

131-
def decode(self, file: IO[bytes]) -> Any:
135+
def decode(self, file: IO[bytes], py_type: type = None) -> Any:
132136
"""
133137
Decode a previously encoded object.
134138
135139
:param file: the io object containing the object to decode
140+
:param py_type: the type that is expected as return type. Needed by some decoders that don't have implicit
141+
type knowledge.
136142
:return: the decoded object
137143
"""
138144
raise NotImplementedError

localstack-core/localstack/state/pickle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class PickleEncoder(Encoder):
237237
def __init__(self, pickler_class: type[dill.Pickler] = None):
238238
self.pickler_class = pickler_class or Pickler
239239

240-
def encode(self, obj: Any, file: BinaryIO):
240+
def encode(self, obj: Any, file: BinaryIO, py_type: type = None) -> Any:
241241
return self.pickler_class(file).dump(obj)
242242

243243

@@ -252,7 +252,7 @@ class PickleDecoder(Decoder):
252252
def __init__(self, unpickler_class: type[dill.Unpickler] = None):
253253
self.unpickler_class = unpickler_class or dill.Unpickler
254254

255-
def decode(self, file: BinaryIO) -> Any:
255+
def decode(self, file: BinaryIO, py_type=None) -> Any:
256256
return self.unpickler_class(file).load()
257257

258258

0 commit comments

Comments
 (0)