Skip to content

Commit 46b9044

Browse files
chore: Switch new type system to use ValueType instead of ValueTypeProto.Enum (#2511)
Signed-off-by: Felix Wang <wangfelix98@gmail.com>
1 parent 7b863d1 commit 46b9044

File tree

3 files changed

+51
-50
lines changed

3 files changed

+51
-50
lines changed

sdk/python/feast/field.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from feast.feature import Feature
1616
from feast.protos.feast.core.Feature_pb2 import FeatureSpecV2 as FieldProto
1717
from feast.types import FeastType, from_value_type
18+
from feast.value_type import ValueType
1819

1920

2021
class Field:
@@ -61,7 +62,8 @@ def __str__(self):
6162

6263
def to_proto(self) -> FieldProto:
6364
"""Converts a Field object to its protobuf representation."""
64-
return FieldProto(name=self.name, value_type=self.dtype.to_value_type())
65+
value_type = self.dtype.to_value_type()
66+
return FieldProto(name=self.name, value_type=value_type.value)
6567

6668
@classmethod
6769
def from_proto(cls, field_proto: FieldProto):
@@ -71,7 +73,8 @@ def from_proto(cls, field_proto: FieldProto):
7173
Args:
7274
field_proto: FieldProto protobuf object
7375
"""
74-
return cls(name=field_proto.name, dtype=from_value_type(field_proto.value_type))
76+
value_type = ValueType(field_proto.value_type)
77+
return cls(name=field_proto.name, dtype=from_value_type(value_type))
7578

7679
@classmethod
7780
def from_feature(cls, feature: Feature):
@@ -81,4 +84,4 @@ def from_feature(cls, feature: Feature):
8184
Args:
8285
feature: Feature object to convert.
8386
"""
84-
return cls(name=feature.name, dtype=from_value_type(feature.dtype.value))
87+
return cls(name=feature.name, dtype=from_value_type(feature.dtype))

sdk/python/feast/types.py

+35-36
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
from enum import Enum
1616
from typing import Dict, Union
1717

18-
from feast.protos.feast.types.Value_pb2 import ValueType as ValueTypeProto
18+
from feast.value_type import ValueType
1919

2020
PRIMITIVE_FEAST_TYPES_TO_VALUE_TYPES = {
21-
"INVALID": "INVALID",
22-
"STRING": "STRING",
21+
"INVALID": "UNKNOWN",
2322
"BYTES": "BYTES",
24-
"BOOL": "BOOL",
23+
"STRING": "STRING",
2524
"INT32": "INT32",
2625
"INT64": "INT64",
27-
"FLOAT32": "FLOAT",
2826
"FLOAT64": "DOUBLE",
27+
"FLOAT32": "FLOAT",
28+
"BOOL": "BOOL",
2929
"UNIX_TIMESTAMP": "UNIX_TIMESTAMP",
3030
}
3131

@@ -40,14 +40,14 @@ def __init__(self):
4040
pass
4141

4242
@abstractmethod
43-
def to_value_type(self) -> ValueTypeProto.Enum:
43+
def to_value_type(self) -> ValueType:
4444
"""
45-
Converts a ComplexFeastType object to the corresponding ValueTypeProto.Enum value.
45+
Converts a ComplexFeastType object to the corresponding ValueType enum.
4646
"""
4747
raise NotImplementedError
4848

4949
def __hash__(self):
50-
return hash(self.to_value_type())
50+
return hash(self.to_value_type().value)
5151

5252
def __eq__(self, other):
5353
return self.to_value_type() == other.to_value_type()
@@ -57,8 +57,7 @@ class PrimitiveFeastType(Enum):
5757
"""
5858
A PrimitiveFeastType represents a primitive type in Feast.
5959
60-
Note that these values must match the values in ValueTypeProto.Enum. See
61-
/feast/protos/types/Value.proto for the exact values.
60+
Note that these values must match the values in /feast/protos/types/Value.proto.
6261
"""
6362

6463
INVALID = 0
@@ -71,12 +70,12 @@ class PrimitiveFeastType(Enum):
7170
BOOL = 7
7271
UNIX_TIMESTAMP = 8
7372

74-
def to_value_type(self) -> ValueTypeProto.Enum:
73+
def to_value_type(self) -> ValueType:
7574
"""
76-
Converts a PrimitiveFeastType object to the corresponding ValueTypeProto.Enum value.
75+
Converts a PrimitiveFeastType object to the corresponding ValueType enum.
7776
"""
7877
value_type_name = PRIMITIVE_FEAST_TYPES_TO_VALUE_TYPES[self.name]
79-
return ValueTypeProto.Enum.Value(value_type_name)
78+
return ValueType[value_type_name]
8079

8180
def __str__(self):
8281
return PRIMITIVE_FEAST_TYPES_TO_STRING[self.name]
@@ -136,11 +135,11 @@ def __init__(self, base_type: Union[PrimitiveFeastType, ComplexFeastType]):
136135

137136
self.base_type = base_type
138137

139-
def to_value_type(self) -> int:
138+
def to_value_type(self) -> ValueType:
140139
assert isinstance(self.base_type, PrimitiveFeastType)
141140
value_type_name = PRIMITIVE_FEAST_TYPES_TO_VALUE_TYPES[self.base_type.name]
142141
value_type_list_name = value_type_name + "_LIST"
143-
return ValueTypeProto.Enum.Value(value_type_list_name)
142+
return ValueType[value_type_list_name]
144143

145144
def __str__(self):
146145
return f"Array({self.base_type})"
@@ -149,33 +148,33 @@ def __str__(self):
149148
FeastType = Union[ComplexFeastType, PrimitiveFeastType]
150149

151150

152-
VALUE_TYPES_TO_FEAST_TYPES: Dict["ValueTypeProto.Enum", FeastType] = {
153-
ValueTypeProto.Enum.INVALID: Invalid,
154-
ValueTypeProto.Enum.BYTES: Bytes,
155-
ValueTypeProto.Enum.STRING: String,
156-
ValueTypeProto.Enum.INT32: Int32,
157-
ValueTypeProto.Enum.INT64: Int64,
158-
ValueTypeProto.Enum.DOUBLE: Float64,
159-
ValueTypeProto.Enum.FLOAT: Float32,
160-
ValueTypeProto.Enum.BOOL: Bool,
161-
ValueTypeProto.Enum.UNIX_TIMESTAMP: UnixTimestamp,
162-
ValueTypeProto.Enum.BYTES_LIST: Array(Bytes),
163-
ValueTypeProto.Enum.STRING_LIST: Array(String),
164-
ValueTypeProto.Enum.INT32_LIST: Array(Int32),
165-
ValueTypeProto.Enum.INT64_LIST: Array(Int64),
166-
ValueTypeProto.Enum.DOUBLE_LIST: Array(Float64),
167-
ValueTypeProto.Enum.FLOAT_LIST: Array(Float32),
168-
ValueTypeProto.Enum.BOOL_LIST: Array(Bool),
169-
ValueTypeProto.Enum.UNIX_TIMESTAMP_LIST: Array(UnixTimestamp),
151+
VALUE_TYPES_TO_FEAST_TYPES: Dict["ValueType", FeastType] = {
152+
ValueType.UNKNOWN: Invalid,
153+
ValueType.BYTES: Bytes,
154+
ValueType.STRING: String,
155+
ValueType.INT32: Int32,
156+
ValueType.INT64: Int64,
157+
ValueType.DOUBLE: Float64,
158+
ValueType.FLOAT: Float32,
159+
ValueType.BOOL: Bool,
160+
ValueType.UNIX_TIMESTAMP: UnixTimestamp,
161+
ValueType.BYTES_LIST: Array(Bytes),
162+
ValueType.STRING_LIST: Array(String),
163+
ValueType.INT32_LIST: Array(Int32),
164+
ValueType.INT64_LIST: Array(Int64),
165+
ValueType.DOUBLE_LIST: Array(Float64),
166+
ValueType.FLOAT_LIST: Array(Float32),
167+
ValueType.BOOL_LIST: Array(Bool),
168+
ValueType.UNIX_TIMESTAMP_LIST: Array(UnixTimestamp),
170169
}
171170

172171

173-
def from_value_type(value_type: ValueTypeProto.Enum,) -> FeastType:
172+
def from_value_type(value_type: ValueType,) -> FeastType:
174173
"""
175-
Converts a ValueTypeProto.Enum to a Feast type.
174+
Converts a ValueType enum to a Feast type.
176175
177176
Args:
178-
value_type: The ValueTypeProto.Enum to be converted.
177+
value_type: The ValueType to be converted.
179178
180179
Raises:
181180
ValueError: The conversion could not be performed.

sdk/python/tests/unit/test_types.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import pytest
22

3-
from feast.protos.feast.types.Value_pb2 import ValueType as ValueTypeProto
43
from feast.types import Array, Float32, String, from_value_type
4+
from feast.value_type import ValueType
55

66

77
def test_primitive_feast_type():
8-
assert String.to_value_type() == ValueTypeProto.Enum.Value("STRING")
8+
assert String.to_value_type() == ValueType.STRING
99
assert from_value_type(String.to_value_type()) == String
10-
assert Float32.to_value_type() == ValueTypeProto.Enum.Value("FLOAT")
10+
assert Float32.to_value_type() == ValueType.FLOAT
1111
assert from_value_type(Float32.to_value_type()) == Float32
1212

1313

1414
def test_array_feast_type():
15-
array_float_32 = Array(Float32)
16-
assert array_float_32.to_value_type() == ValueTypeProto.Enum.Value("FLOAT_LIST")
17-
assert from_value_type(array_float_32.to_value_type()) == array_float_32
18-
1915
array_string = Array(String)
20-
assert array_string.to_value_type() == ValueTypeProto.Enum.Value("STRING_LIST")
16+
assert array_string.to_value_type() == ValueType.STRING_LIST
2117
assert from_value_type(array_string.to_value_type()) == array_string
2218

19+
array_float_32 = Array(Float32)
20+
assert array_float_32.to_value_type() == ValueType.FLOAT_LIST
21+
assert from_value_type(array_float_32.to_value_type()) == array_float_32
22+
2323
with pytest.raises(ValueError):
2424
_ = Array(Array)
2525

@@ -28,8 +28,7 @@ def test_array_feast_type():
2828

2929

3030
def test_all_value_types():
31-
values = ValueTypeProto.Enum.values()
32-
for value in values:
31+
for value in ValueType:
3332
# We do not support the NULL type.
34-
if value != ValueTypeProto.Enum.Value("NULL"):
33+
if value != ValueType.NULL:
3534
assert from_value_type(value).to_value_type() == value

0 commit comments

Comments
 (0)