Skip to content

Commit 07e70cd

Browse files
authored
chore: Relax protobuf constraints (#3103)
* Fix Signed-off-by: Kevin Zhang <kzhang@tecton.ai> * Fix Signed-off-by: Kevin Zhang <kzhang@tecton.ai> * Revert changes Signed-off-by: Kevin Zhang <kzhang@tecton.ai> * Fix Signed-off-by: Kevin Zhang <kzhang@tecton.ai> * Fix lint Signed-off-by: Kevin Zhang <kzhang@tecton.ai> Signed-off-by: Kevin Zhang <kzhang@tecton.ai>
1 parent 69af21f commit 07e70cd

File tree

8 files changed

+294
-177
lines changed

8 files changed

+294
-177
lines changed

sdk/python/feast/proto_json.py

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import uuid
22
from typing import Any, Callable, Type
33

4+
import pkg_resources
45
from google.protobuf.json_format import ( # type: ignore
56
_WKTJSONMETHODS,
67
ParseError,
78
_Parser,
89
_Printer,
910
)
11+
from packaging import version
1012

1113
from feast.protos.feast.serving.ServingService_pb2 import FeatureList
1214
from feast.protos.feast.types.Value_pb2 import RepeatedValue, Value
@@ -15,8 +17,6 @@
1517
JsonObject = Any
1618

1719

18-
# TODO: These methods need to be updated when bumping the version of protobuf.
19-
# https://github.com/feast-dev/feast/issues/2484
2020
def _patch_proto_json_encoding(
2121
proto_type: Type[ProtoMessage],
2222
to_json_object: Callable[[_Printer, ProtoMessage], JsonObject],
@@ -70,7 +70,7 @@ def to_json_object(printer: _Printer, message: ProtoMessage) -> JsonObject:
7070
return value
7171

7272
def from_json_object(
73-
parser: _Parser, value: JsonObject, message: ProtoMessage, path: str
73+
parser: _Parser, value: JsonObject, message: ProtoMessage
7474
) -> None:
7575
if value is None:
7676
message.null_val = 0
@@ -111,7 +111,18 @@ def from_json_object(
111111
"Value {0} has unexpected type {1}.".format(value, type(value))
112112
)
113113

114-
_patch_proto_json_encoding(Value, to_json_object, from_json_object)
114+
def from_json_object_updated(
115+
parser: _Parser, value: JsonObject, message: ProtoMessage, path: str
116+
):
117+
from_json_object(parser, value, message)
118+
119+
# https://github.com/feast-dev/feast/issues/2484 Certain feast users need a higher version of protobuf but the
120+
# parameters of `from_json_object` changes in feast 3.20.1. This change gives users flexibility to use earlier versions.
121+
current_version = pkg_resources.get_distribution("protobuf").version
122+
if version.parse(current_version) < version.parse("3.20"):
123+
_patch_proto_json_encoding(Value, to_json_object, from_json_object)
124+
else:
125+
_patch_proto_json_encoding(Value, to_json_object, from_json_object_updated)
115126

116127

117128
def _patch_feast_repeated_value_json_encoding():
@@ -141,14 +152,29 @@ def _patch_feast_repeated_value_json_encoding():
141152
def to_json_object(printer: _Printer, message: ProtoMessage) -> JsonObject:
142153
return [printer._MessageToJsonObject(item) for item in message.val]
143154

144-
def from_json_object(
155+
def from_json_object_updated(
145156
parser: _Parser, value: JsonObject, message: ProtoMessage, path: str
146157
) -> None:
147158
array = value if isinstance(value, list) else value["val"]
148159
for item in array:
149160
parser.ConvertMessage(item, message.val.add(), path)
150161

151-
_patch_proto_json_encoding(RepeatedValue, to_json_object, from_json_object)
162+
def from_json_object(
163+
parser: _Parser, value: JsonObject, message: ProtoMessage
164+
) -> None:
165+
array = value if isinstance(value, list) else value["val"]
166+
for item in array:
167+
parser.ConvertMessage(item, message.val.add())
168+
169+
# https://github.com/feast-dev/feast/issues/2484 Certain feast users need a higher version of protobuf but the
170+
# parameters of `from_json_object` changes in feast 3.20.1. This change gives users flexibility to use earlier versions.
171+
current_version = pkg_resources.get_distribution("protobuf").version
172+
if version.parse(current_version) < version.parse("3.20"):
173+
_patch_proto_json_encoding(RepeatedValue, to_json_object, from_json_object)
174+
else:
175+
_patch_proto_json_encoding(
176+
RepeatedValue, to_json_object, from_json_object_updated
177+
)
152178

153179

154180
def _patch_feast_feature_list_json_encoding():
@@ -183,12 +209,25 @@ def to_json_object(printer: _Printer, message: ProtoMessage) -> JsonObject:
183209
return list(message.val)
184210

185211
def from_json_object(
186-
parser: _Parser, value: JsonObject, message: ProtoMessage, path: str
212+
parser: _Parser, value: JsonObject, message: ProtoMessage
187213
) -> None:
188214
array = value if isinstance(value, list) else value["val"]
189215
message.val.extend(array)
190216

191-
_patch_proto_json_encoding(FeatureList, to_json_object, from_json_object)
217+
def from_json_object_updated(
218+
parser: _Parser, value: JsonObject, message: ProtoMessage, path: str
219+
) -> None:
220+
from_json_object(parser, value, message)
221+
222+
# https://github.com/feast-dev/feast/issues/2484 Certain feast users need a higher version of protobuf but the
223+
# parameters of `from_json_object` changes in feast 3.20.1. This change gives users flexibility to use earlier versions.
224+
current_version = pkg_resources.get_distribution("protobuf").version
225+
if version.parse(current_version) < version.parse("3.20"):
226+
_patch_proto_json_encoding(FeatureList, to_json_object, from_json_object)
227+
else:
228+
_patch_proto_json_encoding(
229+
FeatureList, to_json_object, from_json_object_updated
230+
)
192231

193232

194233
def patch():

0 commit comments

Comments
 (0)